【发布时间】:2015-06-25 19:54:26
【问题描述】:
我是 Java 新手,我无法弄清楚这一点。我已经尝试在我的对象的参数中传递值,但它只想去没有参数的构造函数。其他 2 有多个参数,但它没有注意到它?提前致谢!
代码如下:
Car vehicle1 = new Car();
Car vehicle2 = new Car();
Car vehicle3 = new Car();
public Car() {
private int numberOfCars;
private String company;
private String modelName;
private String modelYear;
private double hp;
private int doors;
private String gears;
private String color;
private boolean racingCar;
private double price;
numberOfCars = -1;
company = "***EMPTY***";
modelName = "***EMPTY***";
modelYear = "***EMPTY***";
hp = -1;
doors = -1;
gears = "***EMPTY***";
color = "***EMPTY***";
racingCar = false;
price = -1;
}
public Car(String companyName, String modelNa, String modelYe){
numberOfCars = -1;
company = companyName;
modelName = modelNa;
modelYear = modelYe;
hp = -1;
doors = -1;
gears = "***EMPTY***";
color = "***EMPTY***";
racingCar = false;
price = -1;
}
public Car(int carAmount, String companyTitle, String modelTitle, String modelBirth, double horsePower, int door, String gearType, String colors, boolean raceCar, double prices){
numberOfCars = carAmount;
company = companyTitle;
modelName = modelTitle;
modelYear = modelBirth;
hp = horsePower;
doors = door;
gears = gearType;
color = colors;
racingCar = raceCar;
price = prices;
}
【问题讨论】:
-
您是否缺少几行代码?比如无参构造函数?
-
您的代码不完整:您没有包含具有不带参数的构造函数的行。另外,您可以添加 如何 尝试调用其他构造函数吗?声明看起来不错,因此可能与您调用它们的方式有关。
标签: java class object constructor