- //员工类
- abstract class Employee_1
- {
- private String name;
- private String id;
- private double pay;
- Employee_1(String name,String id,double pay)
- {
- this.name = name;
- this.id = id;
- this.pay = pay;
- }
- public abstract void work();
- public String getName()
- {
- return name;
- }
- public String getID()
- {
- return id;
- }
- public double getPay()
- {
- return pay;
- }
- }
- //经理
- class Manager_1 extends Employee_1
- {
- private int bonus;
- Manager_1(String name,String id,double pay,int bonus)
- {
- super(name,id,pay);
- this.bonus = bonus;
-
- }
- public void work()
- {
- System.out.println("manager work");
- System.out.println(getName()+","+getID()+","+getPay()+","+this.bonus);
- }
- }
- //普通员工
- class Pro_1 extends Employee_1
- {
- Pro_1(String name,String id,double pay)
- {
- super(name,id,pay);
-
- }
- public void work()
- {
- System.out.println("pro work");
- System.out.println(getName()+","+getID()+","+getPay());
- }
- }
- class AbstractTest_1
- {
- public static void main(String[] args)
- {
- //调用经理类
- Manager_1 m = new Manager_1("于向东","Manager01",9000,8000);
- m.work();
-
- //调用普通员工类
- Pro_1 p = new Pro_1("zhangsan","keji01",6000);
- p.work();
-
- }
- }
复制代码
我试了下,这么改也是能运行的,但是有点不理解。。。
父类中的三个成员变量name,id,pay已经被私有化了,子类应该继承不到,为什么在子类中getName()方法还能打印出来? 求解释。 就是我理解的Manager_1类中应该是没有name,id,pay成员变量的啊? |