- 编写一个Employee类,声明为抽象类,包含如下三个属性:name,id,salary。
- 提供必要的构造器和抽象方法:work()。
- 对于Manager类来说,他既是员工,还具有奖金(bonus)的属性。
请使用继承的思想,设计CommonEmployee类和Manager类,要求类中提供必要的方法进行属性访问。
Employee.java
package com.klvchen.exer1;
abstract public class Employee {
private String name;
private int id;
private double salary;
public Employee() {
super();
}
public Employee(String name, int id, double salary) {
super();
this.name = name;
this.id = id;
this.salary = salary;
}
public abstract void work();
}
Manager.java
package com.klvchen.exer1;
public class Manager extends Employee{
private double bouns; //奖金
public Manager(double bouns) {
super();
this.bouns = bouns;
}
public Manager(String name, int id, double salary, double bonus) {
super(name, id, salary);
this.bouns = bonus;
}
@Override
public void work() {
System.out.println("管理员工, 提供公司运行的效率");
}
}
CommonEmployee.java
package com.klvchen.exer1;
public class CommonEmployee extends Employee {
@Override
public void work() {
System.out.println("员工在一线车间生产产品");
}
}
EmployeeTest.java
package com.klvchen.exer1;
public class EmployeeTest {
public static void main(String[] args) {
//多态
Employee manager = new Manager(1000);
manager.work();
Employee commonEmployee = new CommonEmployee();
commonEmployee.work();
}
}