class Person{
  
String name = "No name";
public Person(String nm){name = nm;}
}class Employee extends Person{
String empID = "0000"; 
public Employee(String id){empID = id;}
}public class test100 {
public static void main(String[] args) {
Employee e = new Employee("4321"); 
System.out.println(e.empID);
}}
/////////////////////////////////////////////////////////
public Employee(String id){empID = id;}这里为什么会编译出错!!??

解决方案 »

  1.   

    继承概念你还没理解:public Employee(String id)
    {
       super();//这个是隐式调用父类的无参构造函数,但是你父类Persion有了一个有参数的构造函数了,无参的构造函数就没有了,要么在父类中定义一个无参的构造函数,要么在这里使用super(id)来显示声明调用父类有参构造。
       empID = id;
    }这里为什么会编译出错!!??
      

  2.   

    1、修改子类Employeeclass Employee extends Person{
    String empID = "0000";  
    public Employee(String id){
    super(id);//必须写出来调用父类的构造方法
    empID = id;
    }
    }
    2、在Person类中增加一个无参构造函数class Person{
       
    String name = "No name";
    public Person(){}
    public Person(String nm){name = nm;}
    }
      

  3.   

    LS正解,你在继承的时候自己写的构造方法看不出来,但是如果你用编译工具可以自动生成的构造方法的时候就会发现它都会有一条super();的语句,这个就是默认的调用父类的无参构造函数的。
      

  4.   

    public Employee(String id){
    super(id);
    empID = id;
    }