案例演示:
加入我们在开发一个系统时需要对程序员类进行设计,程序员包含3个属性:姓名,工号以及工资
经理,除了含有程序员的属性外,另外还有一个奖金属性
请使用继承的思想设计出程序员类和经理类,要求类中提供必要的方法进行属性访问
代码
class test {
public static void main(String[] args) {
Programmer p = new Programmer("zhangsan","001",10000);
p.work();
Manager m = new Manager("李四","002",20000,10000000);
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;
}
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 abstract void work();
}
class Programmer extends Employee{
public Programmer() {}
public Programmer(String name,String id,double salary) {
super(name,id,salary);
}
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;
}
public void work() {
System.out.println(this.getName() + "..." + this.getId() + "..." + this.getSalary() + "..." + bonus);
}
}
|
|