比如说一个公司有很多部门,那么部门所干的工作都是不一样的,让不同的员工干部同的工作:
- abstract class Employee
- {
- public String name;
- public String id;
- public double pay;
-
- Employee(String name,String id,double pay){
- this.name = name;
- this.id = id;
- this.pay = pay;
- }
-
- public abstract void work();//因为普通员工与经理干的工作不一样,所以这里定义一个抽象类,让不同的员工干不同的工作。
-
- }
-
- class Pro extends Employee
- {
- Pro(String name,String id,double pay){
- super(name,id,pay);
- }
-
- public void work(){
- System.out.println("Pro Work:"+super.name+";id="+super.id+";工资:"+super.pay);
- }
- }
-
- class Manager extends Employee
- {
- private double bonus;
-
- Manager(String name,String id,double pay,double bonus){
- super(name,id,pay);
- this.bonus = bonus;
- }
-
- public void work(){
- System.out.println("Manager Work");
- }
- }
- class AbstractTest
- {
- public static void main(String[] args)
- {
- Pro p = new Pro("zhangsan","123",234.90);
- p.work();
- }
- }
复制代码 |