/* 一、 分析以下需求,并用代码实现:
1.定义项目经理类
属性:
姓名 工号 工资 奖金
行为:
工作work
2.定义程序员类
属性:
姓名 工号 工资
行为:
工作work
要求:向上抽取一个父类,让这两个类都继承这个父类,共有的属性写在父类中,子类重写父类中的方法
编写测试类:完成这两个类的测试 */
测试类
public class test {
public static void main(String[] args) {
Worker w = new Ganager("王经理",11,9000,6000);
Ganager g = (Ganager) w;
System.out.println(g.getName()+" "+g.getNumber()+" "+g.getSalary()+" "+g.getBonus());
g.work();
System.out.println("----------------------------");
System.out.println(g.getName()+" "+g.getNumber()+" "+g.getSalary()+" "+g.getBonus());
g.work();
System.out.println("----------------------------");
Worker w1 = new Coder("小李子",23,8000);
System.out.println(w1.getName()+"--"+w1.getNumber()+"--"+w1.getSalary());
w1.work();
System.out.println("----------------------------");
w1.setName("小李子");
w1.setNumber(23);
w1.setSalary(8000);
System.out.println(w1.getName()+"--"+w1.getNumber()+"--"+w1.getSalary());
w1.work();
}
}
工人类;
public abstract class Worker {
private String name;
private int number;
private int salary;
public abstract void work();
public Worker(){}
public Worker(String name,int number,int salary){
this.name = name;
this.number = number;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
}
public class Ganager extends Worker{
private int bonus;
@Override
public void work() {
System.out.println("检查代码");
}
经理类
public Ganager(){}
public Ganager(String name,int number,int salary,int bonus){
super(name,number,salary);
this.bonus = bonus;
}
public int getBonus() {
return bonus;
}
public void setBonus(int bonus) {
this.bonus = bonus;
}
}
程序员类
public class Coder extends Worker{
@Override
public void work() {
System.out.println("敲代码");
}
public Coder(){}
public Coder(String name,int number,int salary){
super(name,number,salary);
}
}
|
|