【问题标题】:Calling multiple constructors - Only goes to the same one?调用多个构造函数 - 只去同一个?
【发布时间】: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


【解决方案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);
}
}

在使用构造函数之前,您应该了解一些基本事实

  • No Parameter- 编译器将搜索没有参数的 Method/Constructor
  • With Parameter-编译器将搜索带有您在调用时指定的参数的方法/构造函数。
  • 参数的类型数量也很重要

谷歌一下,你会找到理论!

【讨论】:

    【解决方案2】:

    一般来说,如果你写了一个类并且不包含任何构造函数,Java会自动为你提供一个默认构造函数(一个不带参数的),并且虚拟机用一些初始化该类的所有实例变量(如果有的话)默认值(0、null 或 false)。但是,如果您编写带有一些参数的构造函数,并且不编写任何默认构造函数,则 Java 不提供默认构造函数。 see here 这表明您在此处粘贴的代码可能会错过无参数构造函数?

    请注意,要使用参数调用构造函数,您需要在实例化对象时提供参数。

    【讨论】:

      【解决方案3】:

      就像 Guillaume 所说,参数化构造函数需要在实例化对象时使用参数调用。你可以看看这个例子

      http://beginnersbook.com/2014/01/parameterized-constructor-in-java-example/

      【讨论】:

        【解决方案4】:

        为了调用其他构造函数,您需要在构建对象时指定参数:

        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);
        

        【讨论】:

          猜你喜欢
          • 2014-03-12
          • 2012-06-22
          • 2023-04-03
          • 2014-08-01
          • 2016-12-21
          • 2011-03-24
          • 1970-01-01
          • 2012-01-29
          • 1970-01-01
          相关资源
          最近更新 更多