我是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;
}
答案 0 :(得分:1)
为了调用其他构造函数,您需要在构建对象时指定参数:
Car vehicle1 = new Car();
Car vehicle2 = new Car("companyName", "modelNa", "modelYe");
Car vehicle3 = new Car(2, "companyTitle", "modelTitle", "modelBirth", 5.2, 4, "gearType", "colors", false, 1600);
答案 1 :(得分:1)
这是您正面临的问题的完整编码
class Car
{
String company;
String modelName;
String modelYear;
double hp;
int doors;
String gears;
String color;
boolean racingCar;
double price;
int numberOfCars;
public Car() {
numberOfCars = 1;
company = "BMW";
modelName = "BMW M3 DTM";
modelYear = "2012";
hp = 500;
doors = 2;
gears = "Clutch";
color = "Black";
racingCar =true;
price = -1;
}
public Car(String companyName, String modelNa, String modelYe){
numberOfCars = 1;
company = companyName;
modelName = modelNa;
modelYear = modelYe;
hp = 500;
doors = 2;
gears = "Clutch";
color = "White";
racingCar = true;
price = 100000;
}
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;
}
public static void main(String args[])
{
Car vehicle1 = new Car();
Car vehicle2 = new Car("BMW", "BMW M3 DTM", "2012");
Car vehicle3 = new Car(1, "BMW", "BMW M3 DTM", "2012", 500, 2, "Clutch", "White", true, 5000000);
}
}
在使用构造函数之前,您应该了解一些基本事实
答案 2 :(得分:0)
像Guillaume所说,当您实例化对象时,需要使用参数调用参数化构造函数。你可以看到这个例子
http://beginnersbook.com/2014/01/parameterized-constructor-in-java-example/
答案 3 :(得分:0)
通常,如果您编写一个类并且不包含任何构造函数,Java会自动为您提供一个默认构造函数(一个没有参数),并且虚拟机会初始化该类的所有实例变量(如果有的话)默认值(0,null或false)。但是,如果使用某些参数编写构造函数,并且不编写任何默认构造函数,则Java不提供默认构造函数。 see here 表示你在这里粘贴的代码可能会错过一个no-arg构造函数?
请注意,要使用参数调用构造函数,需要在实例化对象时提供参数。