public class Test3 {
/**
* 3 假如我们在开发一个系统时需要对员工进行建模,员工包含 3 个属性:姓名、工号以及工资。
* 经理也是员工,除了含有员工的属性外,另为还有一个奖金属性。
* 请使用继承的思想设计出员工类和经理类。要求类中提供必要的方法进行属性访问。
*/
/**
* 分析:
* 1. 抽象员工类 abstract class Employee
* 2. 员工继承class Code extends Employee
* 3. 经理继承class Manager extends Employee
* @param args
*/
public static void main(String[] args) {
Code code = new Code("顾雨磊","001",5555);
code.work();
Manager m = new Manager("经理","01",10000, 5555);
m.work();
}
}
//抽象员工类
abstract class Employee{
private String name;
private String id;
private double salary;
//空参构造
public Employee() {
}
//有参构造
public Employee(String name, String id, double salary) {
this.name = name;
this.id = id;
this.salary = salary;
}
//set get 方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
//抽象方法
public abstract void work() ;
}
//员工继承
class Code extends Employee{
public Code() {}
public Code(String name, String id, double salary) {
super(name, id, salary);
}
@Override
public void work() {
System.out.println("姓名: "+ this.getName()+"工号: "+this.getId()+"工资: "+this.getSalary());
}
}
//经理继承
class Manager extends Employee{
private int bonus;
public Manager() {}
public Manager(String name, String id, double salary,int bonus) {
super(name, id, salary);
this.bonus = bonus;
}
@Override
public void work() {
System.out.println("姓名: "+ this.getName()+"工号: "+this.getId()+
"工资: "+this.getSalary()+"奖金: "+this.bonus);
}
}