本帖最后由 forward 于 2013-8-25 13:27 编辑
关于private的问题
纠结了好久。父类中没有私有化成员时,整个程序运行编译没问题,父类中的成员变量private后,子类怎么才能访问父类的构造函数进行初始化?麻烦知道的帮忙解答下。谢谢。
代码如下:- <p>//描述雇员</p><p>abstract class Employee
- {
- String name;//<font color="red">就是这三个成员变量加了private之后</font>
- String id;
- double pay;
-
- public void setName(String name)
- {
- this.name = name;
- }
- public String getName()
- {
- return name;
- }
-
- public void setId(String id)
- {
- this.id = id;
- }
- public String getId()
- {
- return id;
- }
-
- public void setPay(double pay)
- {
- this.pay = pay;
- }
- public double getPay()
- {
- return pay;
- }</p><p> Employee(String name,String id,double pay)
- {
- this.name = name;
- this.id = id;
- this.pay = pay;
- }</p><p> public abstract void work();
- }</p><p>//描述程序员
- class Programmer extends Employee
- {
- Programmer(String name, String id,double pay)
- {
- <font color="red">super(name,id,pay);//这里该如何修改呢?</font>
- }</p><p> public void work()
- {
- System.out.println(name+"..."+id+"..."+pay);
- System.out.println("code...");
- }
- }</p><p>
- //描述经理
- class Manager extends Employee
- {
- private int bonus;
- Manager(String name,String id,double pay,int bonus)
- {
- <font color="red"> super(name,id,pay);</font>
- this.bonus = bonus;
- }</p><p> public void work()
- {
- System.out.println(name+"..."+id+"..."+pay+"..."+bonus);
- System.out.println("manage");
- }
- }</p><p>class AbstractTest
- {
- public static void main(String[] args)
- {
- Programmer p = new Programmer("张三","12030413",14500);
- p.work();</p><p> Manager m = new Manager("李四","1012030414",30000,10000);
- m.work();
- }
- }
- </p><p> </p>
复制代码 |