楼主的问题我也遇到过,错误会指向p.name中的name,
因为你Worker类中已经定义了一个Worker类的构造函数Worker(String name,String id,double pay)
可以直接通过Worker p = new Worker("LiSi","一组001",5000)来对你p进行成员变量的赋值
- class Employee //员工
- {
- String name;
- String id;
- double pay;
- /* public Employee()
- {
-
- }*/
- public Employee(String name,String id,double pay)
- {
- this.name = name;
- this.id = id;
- this.pay = pay;
- System.out.println("name is "+this.name);
- System.out.println("id is "+this.id);
- System.out.println("pay is "+this.pay);
- }
- }
- class Worker extends Employee
- {
- public Worker(String name,String id,double pay)// 抽象,
- {
- super(name,id,pay); //继承的父类的
-
- }
- public void biancheng() //自己又定义一个方法,自己独有的方法
- {
- System.out.println("编程");
- }
- }
- class Manager extends Employee
- {
- private double bonus;
- public Manager(String name,String id,double pay,double bonus)
- {
- super(name,id,pay);
- this.bonus=bonus;
-
- }
- public void guangli()
- {
- System.out.println("管理");
- }
- }
- class Test03
- {
- public static void main(String[] args)
- {
-
- Worker p = new Worker("LiSi","一组001",5000);
- }
- }
复制代码
这样系统不会报错 |