This question already has answers here:
Java Error: The constructor is undefined
                            
                                (8个答案)
                            
                    
                去年关闭。
        

    

我创建了两个类,PassCarMotor。我的项目要求我为每个Motor创建一个PassCar实例,但是我很难做到这一点。当我尝试在Motor中创建PassCar的实例时,它不起作用。

我尝试过
Motor motor = new Motor();

private Motor motor = new Motor();

以下是我的PassCar代码
该错误指出构造函数Motor未定义。

public class PassCar extends Vehicle{

    private Motor motor = new Motor();// the error
    private int numPass;
    private boolean AC;

    public PassCar(String make, String model, int year, double price, int numPass, boolean aC, Motor motor) {
        super(make, model, year, price);
        this.numPass = numPass;
        AC = aC;
        this.motor = motor;

    }


    public int getNumPass() {
        return numPass;
    }

    public void setNumPass(int numPass) {
        this.numPass = numPass;
    }

    public boolean isAC() {
        return AC;
    }

    public void setAC(boolean aC) {
        AC = aC;
    }

    public Motor getMotor() {
        return motor;
    }

    public void setMotor(Motor motor) {
        this.motor = motor;
    }


    public void description() {
        System.out.print("In this application, a passenger car is an every day vehicle registered to an individual");
    }

    @Override
    public String toString() {
        String s  = super.toString();
        s += "PassCar numPass = " + numPass + ", AC = " + AC + ", motor = " + motor;
        return s;
    }



}


电机类代码

public class Motor {

    private String name;
    private int cylinders;
    private int bhp;
    private double displacement;

    public Motor(String name, int cylinders, int bhp, double displacement) {
        super();
        this.name = name;
        this.cylinders = cylinders;
        this.bhp = bhp;
        this.displacement = displacement;
    }





    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getCylinders() {
        return cylinders;
    }

    public void setCylinders(int cylinders) {
        this.cylinders = cylinders;
    }

    public int getBhp() {
        return bhp;
    }

    public void setBhp(int bhp) {
        this.bhp = bhp;
    }

    public double getDisplacement() {
        return displacement;
    }

    public void setDisplacement(double displacement) {
        this.displacement = displacement;
    }

    @Override
    public String toString() {
        return "Motor name = " + name + ", cylinders = " + cylinders + ", bhp = " + bhp + ", displacement = " + displacement;
    }

}


代码以打印输入的信息

公共类VehicleTest {

public static void main(String[] args) {

    PassCar p1 = new PassCar("Ford", "Mustang", 2016, 44500.0, 5, true, "EcoBoost", 6, 310, 2.3);
    System.out.print(p1);
}


}

最佳答案

问题在这里

public Motor(String name, int cylinders, int bhp, double displacement) {
    super();
    this.name = name;
    this.cylinders = cylinders;
    this.bhp = bhp;
    this.displacement = displacement;
}


您已经创建了一个带有一些参数的构造函数,但是您尝试在此处不带参数的情况下调用该构造函数

private Motor motor = new Motor();  //cant find constructor that takes no arguments


要解决此问题,您必须与其他参数一起声明一个不带参数的构造函数。

public Motor(){
 //code here
}

关于java - 不知道为什么我的实例出现错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55310084/

10-09 20:31