1、假如我们在开发一个系统时需要对员工进行建模,员工包含 3 个属性:姓名、工号以及工资。经理也是员工,除了含有员工的属性外,另为还有一个奖金属性。请使用继承的思想设计出员工类和经理类。要求类中提供必要的方法进行属性访问。
解题:class Demo_Person {
public static void main(String[] args) {
Manager m = new Manager();
m.setName("张三");
m.setId("007");
m.setSalary(2000);
m.setBonus(200);
System.out.println(m.getName()+"--"+m.getId()+"--"+m.getSalary()+"--"+m.getBonus());
m.work();
Coder c = new Coder("李四","008",1000);
System.out.println(c.getName()+"--"+c.getId()+"--"+c.getSalary());
c.work();
}
}
class Person {
private String name;
private String id;
private double salary;
public Person(){}
public Person(String name,String id,double salary){
this.name = name;
this.id = id;
this.salary = salary;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setId(String id){
this.id = id;
}
public String getId(){
return id;
}
public void setSalary(double salary){
this.salary = salary;
}
public double getSalary(){
return salary;
}
public void work(){
System.out.println("工作");
}
}
class Manager extends Person {
private double bonus;
public Manager(){}
public Manager(String name,String id,double salary,double bonus){
super(name,id,salary);
this.bonus = bonus;
}
public void setBonus(double bonus){
this.bonus = bonus;
}
public double getBonus(){
return bonus;
}
public void work(){
System.out.println("管理");
}
}
class Coder extends Person {
public Coder(){}
public Coder(String name,String id,double salary){
super(name,id,salary);
}
public void work(){
System.out.println("敲代码");
}
} |
|