日期:2014-05-20  浏览次数:20790 次

这个小程序怎么编译不了?
Java code
 class Person {
 String name ="No name";
 public Person(String nm) { name = nm; }
 }

 class Employee extends Person {
 String empID = "0000";
 [color=#FF0000]public Employee(String id) { empID = id; }//这里有什么错误阿?[/color]  
 }
 public class wren4255 { 
     public static void main(String[] args) {
 Employee e = new Employee("4321");
 System.out.println(e.empID);
 }
 }


------解决方案--------------------
答:父类Person 中你要定义一个默认构造器。
这是因为:
public Employee(String id) { empID = id; }
在编译时就是:
public Employee(String id) 

super();//这是编译程序自动加上的。而你Person中又没有定义无参的构造器。所以出错了。因此:你自己或者明确调用:super("...");
empID = id; 
}

------解决方案--------------------
Java code

class Person {
 String name ="No name";
 public Person(String nm) { name = nm; }
 }

 class Employee extends Person {
 String empID = "0000";
 public Employee(String id) { 
     super(id);
     empID = id; 
     }
 }
 public class wren4255 { 
     public static void main(String[] args) {
 Employee e = new Employee("4321");
 System.out.println(e.empID);
 }
 }

------解决方案--------------------
Java code

class Person {
 String name ="No name";
 public Person(){};
 public Person(String nm) { name = nm; }
 }

 class Employee extends Person {
 String empID = "0000";
 public Employee(String id) { 
     
     empID = id; 
     }
 }
 public class wren4255 { 
     public static void main(String[] args) {
 Employee e = new Employee("4321");
 System.out.println(e.empID);
 }
 }

------解决方案--------------------
探讨
答:父类Person 中你要定义一个默认构造器。
这是因为:
public Employee(String id) { empID = id; }
在编译时就是:
public Employee(String id)
{
super();//这是编译程序自动加上的。而你Person中又没有定义无参的构造器。所以出错了。因此:你自己或者明确调用:super("...");
empID = id;
}

------解决方案--------------------
Java code

class Person {
 String name ="No name";
 public Person(String nm) { name = nm; }
 }

 class Employee extends Person {
 String empID = "0000";
 [color=#FF0000]public Employee(String id) { empID = id; [/color]  }//这里有什么错误阿?

[color=#FF0000]public Employee(String id) { super(id);empID = id; [/color]
 }
 public class wren4255 { 
     public static void main(String[] args) {
 Employee e = new Employee("4321");
 System.out.println(e.empID);
 }
 }