2013-06-20 170 views
-4

这是测试类,我有一个错误,我不知道它是什么。Java测试类错误

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; 
} 


} 
+5

你怎么能告诉你有一个错误?!堆栈跟踪?或者是你的ide标志着什么? –

+0

给我们一个线索:你看到什么错误? – DaveH

回答

8

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

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

陈述

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

是那么多余的,可以删除。

+0

好的观察 – Mubin

+0

该死的...... 3秒太晚了;-) –

+0

我得到的错误是在Employee e = new Employee(); – user2478522

1

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

0

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

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

创建构造

Employee(String id, String lastname) 

{ 

,并使用像

Employee e = new Employee(); //not possible 
+0

现在即时我得到这个错误,当我运行该程序: – user2478522

+0

@ user2478522请参阅Reimus回答,使用它。 –

+0

java.lang.UnsupportedClassVersionError:EmployeeTest:不受支持的major.minor版本51.0 \t 找不到主类:EmployeeTest。程序将会退出。 线程“main”中的异常Java结果:1 – user2478522

0

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

这是默认的构造函数:如果你定义了至少有一个构造明确,不生成默认的构造函数

Employee e = new Employee(); 

Employee() {} 

然后你就可以像实例化对象。