不知道为什么我的实例出现错误

时间:2019-03-23 02:38:39

标签: java

我创建了两个类,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);
}

}

3 个答案:

答案 0 :(得分:1)

问题在这里

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
}

答案 1 :(得分:0)

尝试在Motor类中添加以下代码

公共汽车(){ }

答案 2 :(得分:0)

将空的参数构造函数添加到Motor类。像这样;

public Motor(){

}