问题描述
1、假如我们在开发一个系统时需要对员工进行建模,【员工Employee】包含3个属性:姓名name、工号id以及工资salary;【经理Manager】也是员工,除了含有员工的属性外,另为还有一个奖金属性bonus。 请使用继承的思想设计出员工类和经理类,要求类中提供必要的方法进行属性访问,并能够一次性输出对象的基本信息。
完整代码
public class Main {
public static void main(String[] args) {
Employee e1 = new Employee("Zhangsan",20202001,8800);
Manager m1 =new Manager("Lisi",20203001,9800,12000);
e1.show();
m1.show();
}
static class Employee {
private String name;
private int ID;
private double salary;
public Employee(){}
public Employee(String name,int ID,double salary){
this.name=name;
this.ID=ID;
this.salary=salary;
}
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;
}
void show(){
System.out.println("Name: "+name+", "+"Job number: " +ID+", "+"Salary: "+salary);
}
}
static class Manager extends Employee {
private double bonus;
public Manager(String name,int ID,double salary,double bonus){
super(name,ID,salary);
this.bonus=bonus;
}
public double getBonus() {
return bonus;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
@Override
void show() {
System.out.println("Name: "+super.name+", "+"Job number: " +super.ID+", "+"Salary: "+super.salary+", "+"Bonus: "+bonus);
}
}
}
运行结果
|