01.abstract class Employee
02.{
03.private String name;
04.private String id;
05.private int pay;
06.Employee(String name,String id,int pay)
07.{
08.this.name=name;
09.this.id=id;
10.this.pay=pay;
11.}
12.public abstract void work();//这里定义一个抽象方法的原因是员工都有姓名年龄等,但是员工又分为了程序员合经理等,他们的工作职位都不一样,干事的事也不一样,不能确定,所以定义一个抽象方法,让子覆盖该抽象方法
13.}
14.
15.
16.class Manager extends Employee
17.{
18.private int bonus;
19.Manager(String name,String id,int pay,int bonus)
20.{
21.super(name,id,pay);
22.this.bonus=bonus;
23.}
24.
25.public void work()
26.{
27.System.out.println("manager work");
28.}
29.
30.
31.}
32.
33.class Pro extends Employee
34.{
35.Pro(String name,String id,int pay)
36.{
37.super(name,id,pay);
38.}
39.public void work()
40.{
41.System.out.println("pro work");
42.}
43.}
已经有明确了程序员和经理,已经通过构造函数初始化了,接下来就是创建对象了,
创建员工对象,
pro p = new pro("zhangsan","39",1233445);
p.work();
创建经理对象
manger m = new manger("wangwu","43",3898e8);
m.work(); |