interface IPayable{
public int getSalary();
}
class ThirdCompany implements IPayable{
@Override
public int getSalary() {
return 100000;
}
}
abstract class Employee implements IPayable{
private String name;
public String getName() {
return name;
}
public Employee(String name)
{
this.name = "@" + name + "@" ;
}
public abstract int getSalary();
}
class Developer extends Employee{
private int salary;
public Developer(String name, int salary)
{
super(name);
this.salary = salary;
}
@Override//方法重写注解
public int getSalary() {
return salary;
}
}
class HourlyWorker extends Employee{
private int hours;//这个月工作了多少小时
public int getHours() {
System.out.println(this.getClass().getSimpleName());
return hours;
}
public HourlyWorker(String name, int hours)
{
super(name);
this.hours = hours;
}
@Override
public int getSalary() {
return 30*hours;
}
}
public class Main2 {
// static void paySalary(Developer developer){
// System.out.println(developer.getSalary());
// }
static void paySalary(IPayable hourlyWorker){
System.out.println(hourlyWorker.getSalary());
}
public static void main(String[] args) {
IPayable developer = new Developer("张二",5000);
IPayable hourlyWorker = new HourlyWorker("冯六",200);
IPayable thirdCompany = new ThirdCompany();
Employee employee = new HourlyWorker("wangwu",11);
System.out.println(((HourlyWorker)hourlyWorker).getHours());
paySalary(thirdCompany);
paySalary(developer);
paySalary(hourlyWorker);
}
}
06-12
924
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
06-07
1367
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-21
1779
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)