-4

这是我有错误的测试类,我无法弄清楚它到底是什么。

import java.util.*;
public class EmployeeTest 
{

public static void main(String[] args) {
    Employee e = new Employee();
    e.setId("100012");
    e.setLastname("Smith"); 
    ResponsibilityDecorator d;
    d = new Recruiter(e);
    d = new CommunityLiaison(e);
    d = new ProductionDesigner(e);      
    System.out.println(e.toString());
}
}

这是链接到测试类的类

public class Employee 
{

String id;
String lastname;

Employee(String id, String lastname) 

{

    this.id=id;
    this.lastname=lastname;

}
 EmploymentDuties eduties=new EmploymentDuties();

public EmploymentDuties getDuties()
{
    return eduties;
}
public String toString(){
    return "Duties for this employee: "+eduties.jobtitles;
}

public void setId(String id)
{
    this.id = id;
}

public void setLastname(String lastname)
{
    this.lastname = lastname;
}


}
4

4 回答 4

8

中没有无参数构造函数Employee。添加参数以使用现有的构造函数EmployeeTest

Employee e = new Employee("100012", "Smith");

声明

e.setId("100012");
e.setLastname("Smith");

然后是多余的,可以删除。

于 2013-06-20T14:25:26.800 回答
1

您的 Employee 类恰好定义了一个构造函数:Employee(String, String)。确保从 EmployeeTest 调用它或定义无参数构造函数。

于 2013-06-20T14:27:14.623 回答
0

默认构造函数是java提供的构造函数,没有你提供的构造函数。

一旦提供了任何构造函数,就不再提供默认构造函数。

你创建了构造函数

Employee(String id, String lastname) 

{

并使用喜欢

Employee e = new Employee();  //not possible
于 2013-06-20T14:31:24.967 回答
0

默认构造函数是自动生成的无参数构造函数,除非您定义另一个构造函数。

这是默认构造函数:

 Employee() {}

然后您可以实例化对象,例如:

Employee e = new Employee();

如果显式定义了至少一个构造函数,则不会生成默认构造函数。

于 2013-06-20T14:32:42.363 回答