总的抽象类Employee
在总的类里定义私有变量,提供接口,构造无参方法,相当于构造一个javabean
public abstract class Employee {
private String name;
private int id;
public Employee(){
}
public Employee(String name, int id) {
this.name = name;
this.id = id;
}
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 abstract void work();
}
子类继承父类Developer类
public abstract class Developer extends Employee {
}
Maintainer类
public abstract class Maintainer extends Employee {
}
再有子类继承父类 这时有具体的成员方法 所以不是抽象类,但是要重写父类的方法
继承Developer类的Javaee类和Android类
public class Javaee extends Developer {
@Override
public void work() {
System.out.println("员工号为:"+getId()+"的"+getName()+"正在开发网站");
}
}
public class Android extends Developer{
@Override
public void work() {
System.out.println("员工号为:"+getId()+"的"+getName()+"正在开发App ");
}
}
继承Maintainer类的Network和Hardware类
public class Network extends Maintainer{
@Override
public void work() {
System.out.println("员工号为:"+getId()+"的"+getName()+"正在开发网络");
}
}
public class Hardware extends Maintainer {
@Override
public void work() {
System.out.println("员工号为:"+getId()+"的"+getName()+"正在开发硬件");
}
}
测试类Test
public class Test {
public static void main(String[] args) {
Javaee javaee=new Javaee();
javaee.setName("李琦");
javaee.setId(1);
Android android=new Android();
android.setName("淡萌");
android.setId(2);
Network network=new Network();
network.setId(3);
network.setName("张大琦");
Hardware hardware=new Hardware();
hardware.setId(4);
hardware.setName("龙奕超");
javaee.work();
android.work();
network.work();
hardware.work();
}
}