Java测试类中的错误
这是一个测试类,我有一个错误,我不能弄清楚它到底是什么Java测试类中的错误,java,decorator,Java,Decorator,这是一个测试类,我有一个错误,我不能弄清楚它到底是什么 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 Rec
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;
}
}
Employee
中没有参数构造函数。在EmployeeTest
Employee e = new Employee("100012", "Smith");
声明
e.setId("100012");
e.setLastname("Smith");
然后是多余的,可以删除。您的类Employee只定义了一个构造函数:Employee(String,String)。确保从EmployeeTest调用它或定义一个无参数构造函数。默认构造函数是java提供的构造函数,而您没有提供任何构造函数 提供任何构造函数后,将不再提供默认构造函数 您创建了构造函数
Employee(String id, String lastname)
{
和使用一样
Employee e = new Employee(); //not possible
默认构造函数是自动生成的无参数构造函数,除非您定义另一个构造函数。 这是默认构造函数:
Employee() {}
然后您可以实例化如下对象:
Employee e = new Employee();
如果您明确定义了至少一个构造函数,则不会生成默认构造函数。您如何判断有错误?!stacktrace?或者您的ide标记了什么?给我们一个线索:您看到的错误是什么?提供如上所示的预期参数。这样做之后,实际上不需要调用
setId
和setLastname
:)很好。不要忘记,如果所有问题都已解决:)@user2478522请参阅Reimus答案,以使用它。java.lang.UnsupportedClassVersionError:EmployeeTest:Unsupported major.minor version 51.0找不到主类:EmployeeTest。程序将退出。线程“main”Java结果中出现异常:1