未定义的构造函数,但我有一个默认的构造函数?

时间:2015-06-28 04:42:56

标签: java

Eclipse说:

  

线程“main”中的异常java.lang.Error:未解析的编译   问题:构造函数Car(String,String,int)未定义   构造函数Car(String,String,int)未定义方法   getCarInfo()未定义类型Car方法getCarInfo()是   未定义的Car类型

     

at CarDriver.main(CarDriver.java:5)

这是我的司机:

public class CarDriver {

    public static void main(String[] args) {
        Car c1 = new Car("Nissan", "Z31", 175);
        Car c2 = new Car("Honda", "Prelude", 145);

        System.out.println(c1.getCarInfo());
        System.out.println(c2.getCarInfo());
    }
}

这是我的班级:

public class Car {

    /*
     * Car class is a method to make a car with make and model of
     * String and odometer of int to reflect how fast or how far
     * the odometer reads, I guess.
     */

    private String make;
    private String model;
    private int odometer;          

    public Car() {  //This is the default constructor for the Car.
        make = "Toyota";
        model = "Supra"; //My favorite car, especially the 80s ones. (*•̀ᴗ•́*)و ̑̑
        odometer = 225;
    }

    public Car(String make, String model, int odometer) {
        this.make = make;
        this.model = model;
        this.odometer = odometer;
    }

    public void setMake(String manufacturer) {
        this.make = manufacturer;
        System.out.println("The car is made by" + this.make);
    }

    public void setModel(String type) {
        this.model = type;
        System.out.println("The car is a " + this.model);
    }

    public void setSpeed(int speed) {
        this.odometer = speed;
        System.out.println("The car goes " + this.odometer + " KPH");
    }

    public String getCarInfo() {
        String carDescription = "The car is a " + model + " made by " + make + " and goes " + odometer + " KPH";
        return carDescription;
    }
}

不确定是错的 谢谢你的帮助!

1 个答案:

答案 0 :(得分:-1)

您的Eclipse可能会感到困惑。有时IDE会让部分构建卡在内存中并且似乎无法清除它们。我知道这听起来像是一个警察,但是你试过告诉Eclipse清理,完全关闭Eclipse,再次启动它,并告诉它进行完全重建吗?当事情退出有意义时我会这样做。自改变IDE以来,我不得不少做这件事,但有时候仍然会发生。

哦,另一种可能性(不太可能,但可能):你是否可以在Eclipse将其作为库提取的地方编译早期版本的类?如果您正在混合使用命令行工作和Eclipse工作,则可能会发生这种情况。告诉它忽略不受其控制的任何构建目录,并检查其导入是否存在任何可疑内容。