Java测试类获胜';不编译
我写了四个协作类,其中一个叫做Java测试类获胜';不编译,java,test-class,Java,Test Class,我写了四个协作类,其中一个叫做工作站: 这是工作站类的代码 import java.util.ArrayList; public class WorkStation { /** * the pc */ private Hardware pc; /** * list of available software */ private ArrayList<Software> applications;
工作站:
这是工作站
类的代码
import java.util.ArrayList;
public class WorkStation {
/**
* the pc
*/
private Hardware pc;
/**
* list of available software
*/
private ArrayList<Software> applications;
/**
* whether a web camera is attached or not
*/
private boolean hasWebCamera;
/**
* id number of the Workstation
*/
private final int idNumber;
/*
* Constructor with three parameters
*/
public WorkStation(Hardware pc, boolean hasWebCamera, int idNumber) {
this.pc = pc;
this.hasWebCamera = hasWebCamera;
this.idNumber = idNumber;
applications = new ArrayList<Software>();
}
//------------------------------- Getter Methods ------------------------------
/*
* Gets the pc
*
* @return The pc
*/
public Hardware getPc() {
return pc;
}
/*
* Gets whether there is a web camera
*
* @return True or false
*/
public boolean isHasWebCamera() {
return hasWebCamera;
}
/*
* Gets the id number
*
* @return The id number
*/
public double getIdNumber() {
return idNumber;
}
//--------------------------- Setter Methods -------------------------
/*
* Sets the pc
*
* @param The pc
*/
public void setPc(Hardware pc) {
this.pc = pc;
}
/*
* Sets whether there is a web camera
*
* @param True or false
*/
public void setHasWebCamera(boolean hasWebCamera) {
this.hasWebCamera = hasWebCamera;
}
// --------------------- ArrayList Methods --------------------
/*
* Method to add a piece of software to the list
*/
public void addSoftware(Software software) {
applications.add(software);
}
/*
* Method to remove a piece of software from the list
*/
public void removeSoftware(Software software) {
applications.remove(software);
}
/*
* Method to remove all software from the list
*/
public void clearApplications(Software software) {
applications.clear();
}
/*
* toString Method
*
* @param aPc
*
* @param aHasWebCamera
*
* @param aIdNumber
*/
public String toString() {
return "Pc is " + getPc() + ", web camera is " + isHasWebCamera() +
" and ID number is " + getIdNumber();
}
}//end of class
我遇到的问题是,当我试图编译测试类中的内容时,会出现一条错误消息,上面说:
cannot find symbol
symbol : constructor WorkStation() location: class WorkStation:
WorkStation w1 = new WorkStation();
我真的不明白为什么会这样。工作站
类编译得很好,而测试
类中的其他类编译得很好,所以有人能看到为什么工作站
类不能在测试
类中编译吗 原因是这一行:
WorkStation w1 = new WorkStation();
您的工作站
类未包含任何无参数构造函数。如果类中已经定义了参数构造函数,Java编译器不会在类中插入默认构造函数。因此,您必须在工作站类中定义一个无参数构造函数。您定义了一个构造函数:
import java.util.ArrayList;
public class WorkStation {
/**
* the pc
*/
private Hardware pc;
/**
* list of available software
*/
private ArrayList<Software> applications;
/**
* whether a web camera is attached or not
*/
private boolean hasWebCamera;
/**
* id number of the Workstation
*/
private final int idNumber;
/*
* Constructor with three parameters
*/
public WorkStation(Hardware pc, boolean hasWebCamera, int idNumber) {
this.pc = pc;
this.hasWebCamera = hasWebCamera;
this.idNumber = idNumber;
applications = new ArrayList<Software>();
}
//------------------------------- Getter Methods ------------------------------
/*
* Gets the pc
*
* @return The pc
*/
public Hardware getPc() {
return pc;
}
/*
* Gets whether there is a web camera
*
* @return True or false
*/
public boolean isHasWebCamera() {
return hasWebCamera;
}
/*
* Gets the id number
*
* @return The id number
*/
public double getIdNumber() {
return idNumber;
}
//--------------------------- Setter Methods -------------------------
/*
* Sets the pc
*
* @param The pc
*/
public void setPc(Hardware pc) {
this.pc = pc;
}
/*
* Sets whether there is a web camera
*
* @param True or false
*/
public void setHasWebCamera(boolean hasWebCamera) {
this.hasWebCamera = hasWebCamera;
}
// --------------------- ArrayList Methods --------------------
/*
* Method to add a piece of software to the list
*/
public void addSoftware(Software software) {
applications.add(software);
}
/*
* Method to remove a piece of software from the list
*/
public void removeSoftware(Software software) {
applications.remove(software);
}
/*
* Method to remove all software from the list
*/
public void clearApplications(Software software) {
applications.clear();
}
/*
* toString Method
*
* @param aPc
*
* @param aHasWebCamera
*
* @param aIdNumber
*/
public String toString() {
return "Pc is " + getPc() + ", web camera is " + isHasWebCamera() +
" and ID number is " + getIdNumber();
}
}//end of class
public WorkStation(Hardware pc, boolean hasWebCamera, int idNumber){
this.pc = pc;
this.hasWebCamera = hasWebCamera;
this.idNumber = idNumber;
applications = new ArrayList<Software>();
}
public WorkStation(){...}
您需要定义一个构造函数:
public WorkStation(Hardware pc, boolean hasWebCamera, int idNumber){
this.pc = pc;
this.hasWebCamera = hasWebCamera;
this.idNumber = idNumber;
applications = new ArrayList<Software>();
}
public WorkStation(){...}
默认构造函数是自动生成的无参数构造函数,除非您定义另一个构造函数。它将所有未初始化的字段初始化为其默认值。由于您定义了一个构造函数,因此没有生成默认构造函数…您的工作站类只有一个构造函数,它包含3个参数。您正在尝试使用一个不存在的无参数构造函数
如果您没有定义任何构造函数,则会为您添加一个无参数构造函数,但一旦您定义了至少一个构造函数,则不会创建默认的无参数构造函数,如果您需要,则必须定义它。WorkStation
没有无参数构造函数。添加它或使用它拥有的构造函数。