继承extends&抽象类abstract

2018-11-03  本文已影响0人  所以然WZY

测试类

package com.pp;

public class Test {                  
    public static void main(String[] args) {
        JavaEE javaEE = new JavaEE();
        javaEE.setName("张三");
        javaEE.setNum(1001);
        javaEE.work();

        Android android = new Android();
        android.setName("李四");
        android.setNum(1002);
        android.work();

        network network = new network();
        network.setName("王五");
        network.setNum(1003);
        network.work();

        Hardware hardware = new Hardware();
        hardware.setName("赵六");
        hardware.setNum(1004);
        hardware.work();

    }
}

员工类


public abstract class Employee {
     private String name;
     private int num;

   abstract void work();

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }
}

研发部员工

package com.pp;

public abstract class Developer extends Employee {
}

维护部员工

package com.pp;

public abstract class Maintainer extends Employee {

}

JavaEE员工


public class JavaEE extends Developer {

    @Override
    void work() {
        System.out.println("员工号为"+getNum()+"    姓名为"+getName()+"的员工,正在研发淘宝网站");

    }
}

Android员工

package com.pp;

public class Android extends Developer {

    @Override
    void work() {
        System.out.println("员工号为"+getNum()+"    姓名为"+getName()+"的员工,正在研发淘宝手机客户端软件");

    }
}

网络维护员工

package com.pp;

public class network extends Maintainer {

    @Override
    void work() {
        System.out.println("员工号为"+getNum()+"    姓名为"+getName()+"的员工,正在检查网络是否畅通");

    }
}

硬件维护员工

package com.pp;

public class Hardware extends Maintainer {

    @Override
    void work() {
        System.out.println("员工号为"+getNum()+"    姓名为"+getName()+"的员工,正在修复打印机");

    }
}

编译结果如下图所示

员工号为1001    姓名为张三的员工,正在研发淘宝网站
员工号为1002    姓名为李四的员工,正在研发淘宝手机客户端软件
员工号为1003    姓名为王五的员工,正在检查网络是否畅通
员工号为1004    姓名为赵六的员工,正在修复打印机

Process finished with exit code 0
上一篇 下一篇

猜你喜欢

热点阅读