需求
编写一个Employee类,声明方法为抽象类,包含:name、id、salary三个属性。提供必要的构造器和抽象方法:work()。对于Manager类来说,他既是员工,还具有奖金(bonnus)的属性。请使用继承的思想,设计CommonEmployee类和Manager类,要求类中提供必要的方法进行属性访问,实现work()。【继承+抽象】
实现
Employee
package test;
abstract public class Employee {
private String name;
private int id;
private double salary;
public Employee(String name, int id, double salary) {
this.name = name;
this.id = id;
this.salary = salary;
}
//将work做成一个抽象方法
public abstract void work();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
Manager
package test;
public class Manager extends Employee {
private double bonus;
public Manager(String name, int id, double salary) {
super(name, id, salary);
}
public double getBonus() {
return bonus;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
@Override
public void work() {
System.out.println("经理 " + getName() + " 工作中...");
}
}
CommonEmployee
package test;
public class CommonEmployee extends Employee{
public CommonEmployee(String name, int id, double salary) {
super(name, id, salary);
}
@Override
public void work() {
System.out.println("普通员工 " + getName() + " 工作中...");
}
}
Main
package test;
public class Main {
public static void main(String[] args) {
Manager wty2002 = new Manager("WTY2002", 1, 500000);
wty2002.setBonus(1000);
wty2002.work();
CommonEmployee tom = new CommonEmployee("Tom", 2, 2000);
tom.work();
}
}