#1.有父类Employee,有成员经理manager和程序员coder
#因为父类中有共同的name,salary,id,work(),但是各不相同,所以方法可以定义为抽象方法
#其中Manager继承Employee, 有bonus和work()
#coder继承Employee, 有work()
#父类Employee
public abstract class Employee {
private String name;
private double salary;
private String id;
public Employee() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Employee(String name, double salary, String id) {
this.name = name;
this.salary = salary;
this.id = id;
}
public abstract void work();
}
}
#子类Manager类
public class Manager extends Employee {
private int bonus;
public int getBonus() {
return bonus;
}
public void setBonus(int bonus) {
this.bonus = bonus;
}
public Manager() {
}
public Manager(String name, double salary, String id, int bonus) {
super(name, salary, id);
this.bonus = bonus;
}
@Override
public void work() {
System.out.println("经理在工作");
}
}
#子类coder类
public class Coder extends Employee {
@Override
public void work() {
System.out.println("程序员要敲代码");
}
}
#测试运行
public class TestDemo1 {
public static void main(String[] args) {
Employee em = new Coder();
em.work();
Employee em2 = new Manager("王五",60000,"研发部02",50000);
em2.work();
Coder c = new Coder();
c.setName("张三");
c.setSalary(30000);
c.setId("研发部");
System.out.println("姓名:"+c.getName());
System.out.println("薪水:"+c.getSalary());
System.out.println("部门:"+c.getId());
Manager m = new Manager("李四",40000,"研发部01",10000);
System.out.println("姓名:"+m.getName());
System.out.println("薪水:"+m.getSalary());
System.out.println("部门:"+m.getId());
System.out.println("奖金:"+m.getBonus());
}
}