需求:
1。 经理
成员变量:工号,姓名,工资,管理奖金
成员方法:工作(管理其他人),吃饭(吃米饭)
2。厨师
成员变量:工号,姓名,工资
成员方法:工作(炒菜),吃饭(吃米饭)
父类–员工
public class Employee {
private String id;
private String name;
private double salary;
public Employee() {
}
public Employee(String id, String name, double salary) {
this.id = id;
this.name = name;
this.salary = salary;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
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 void work() {
System.out.println("员工在工作");
}
public void eat() {
System.out.println("吃米饭");
}
}
子类–经理
public class Manager extends Employee {
private double bouns;
public Manager() {
}
public Manager(String id, String name, double salary, double bouns) {
super(id, name, salary);
this.bouns = bouns;
}
public double getBouns() {
return bouns;
}
public void setBouns(double bouns) {
this.bouns = bouns;
}
@Override
public void work() {
System.out.println("管理其他人");
}
}
子类–厨师
public class Cooker extends Employee {
public Cooker() {
}
public Cooker(String id, String name, double salary) {
super(id, name, salary);
}
@Override
public void work() {
System.out.println("厨师正在炒菜");
}
}
测试类
public class Test {
public static void main(String[] args) {
Manager m = new Manager("001", "朵朵", 1000, 200);
System.out.println(m.getId() + "," + m.getName() + "," + m.getSalary() + "," + m.getBouns());
m.work();
m.eat();
System.out.println("----------------------");
Cooker c = new Cooker("002", "小七", 800);
System.out.println(c.getId() + "," + c.getName() + "," + c.getSalary());
c.work();
c.eat();
}
}
输出
001,朵朵,1000.0,200.0
管理其他人
吃米饭
----------------------
002,小七,800.0
厨师正在炒菜
吃米饭