0

我写了四个合作类,其中一个叫做WorkStation

这是WorkStation该类的代码。

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

然后我必须在一个测试类中测试合作类。

这是Test到目前为止该类的代码。

public class Test{

    public static void main(String[] args){

    //Software
    Software s1 = new Software();
    s1.setName("Database");
    s1.setManufacturer("Microsoft");
        System.out.println(s1.toString());

    Software s2 = new Software("Drawing Package", "Sony");
    System.out.println(s2.toString());

    //Hardware
    Hardware h1 = new Hardware();
    h1.setManufacturer("Dell");
    h1.setProcessorType("Dual Core");
    h1.setHardDiskCapacity(1);
        System.out.println(h1.toString());

    Hardware h2 = new Hardware("Oracle", "Intel Core i7", 3);
        System.out.println(h2.toString());

    //WorkStation
    WorkStation w1 = new WorkStation();

我遇到的问题是,当我尝试编译测试类中的内容时,会出现一条错误消息:

cannot find symbol
symbol  : constructor WorkStation() location: class WorkStation: 
WorkStation w1 = new WorkStation();

我真的不明白为什么会这样。该类WorkStation完美编译,并且该类中的其他类已Test编译,所以任何人都可以看到为什么WorkStation该类不会在Test该类中编译?

4

3 回答 3

2

原因是这条线:

WorkStation w1 = new WorkStation();

您的WorkStation类不包含任何无参数构造函数。如果您已经在该类中定义了参数构造函数,Java 编译器不会在您的类中插入默认构造函数。所以你必须在你的类中定义一个无参数的构造函数WorkStation

于 2013-07-29T20:32:32.453 回答
0

您定义了一个构造函数:

public WorkStation(Hardware pc, boolean hasWebCamera, int idNumber){

        this.pc = pc;
        this.hasWebCamera = hasWebCamera;
        this.idNumber = idNumber;
        applications = new ArrayList<Software>();

}

但你打电话给:

WorkStation w1 = new WorkStation();

您需要定义一个构造函数:

public WorkStation(){...}

默认构造函数是自动生成的无参数构造函数,除非您定义另一个构造函数。它将任何未初始化的字段初始化为其默认值。由于您定义了构造函数,因此没有生成默认构造函数...

于 2013-07-29T20:32:34.300 回答
0

您的工作站类只有一个构造函数,它需要 3 个参数。您正在尝试使用不存在的无参数构造函数。

如果您没有定义任何构造函数,则会为您添加一个无参数的构造函数,但只要您定义了至少一个构造函数,就不会创建默认的无参数构造函数,如果您想要一个,则必须定义它。

于 2013-07-29T20:33:33.787 回答