对给出需求进行数据建模 需求:
1、公司中程序员有姓名、工号、薪水、工作内容(编码)。
2、项目经理除了有姓名、工号、薪水、还有奖金、工作内容(管理)。
分析:程序员和经理不存在着直接继承关系,但是程序员和经理却具有共性内容。可以进行抽取。因为他们都是公司的雇员可以将程序员和经理进行抽取,建立体系。
创建Employee类
public abstract class Employee {
String name;
String id;
double pay;
public Employee(String name, String id, double pay) {
this.name = name;
this.id = id;
this.pay = pay;
}
public abstract void work();
}
class Programmer extends Employee{
public Programmer(String name, String id, double pay) {
super(name, id, pay);
}
public void work(){
System.out.println("程序员疯狂编码");
}
}
class Manager extends Employee{
private double bouns;
public Manager(double bouns, String name, String id, double pay) {
super(name, id, pay);
this.bouns = bouns;
}
public void work(){
System.out.println("项目经理谨慎管理");
}
}
创建Test类
public class Test {
public void workTest(Employee e){
e.work();
}
public static void main(String[] args) {
Test t = new Test();
t.workTest(new Programmer("码农","01",20));
t.workTest(new Manager(10000,"管理","02",20));
}
}