我在我的Java编程课程中坚持了最后的一次钻研,并且希望能得到关于我应该做什么才能满足所需结果的指导。

该代码将通过以下不可更改的代码进行测试:

Test:
int wheels = 4;
String make = "Honda";
String color = "Yellow";
Vehicle v1 = new Vehicle(make);
System.out.println(v1.aboutMe());
Vehicle v2 = new Vehicle(make, color);
System.out.println(v2.aboutMe());
Vehicle v3 = new Vehicle(make, color, wheels);
System.out.println(v3.aboutMe());
Result:
Vehicle object created.
Make: Honda
Colour: Blue
Wheels: 4
Vehicle object created.
Make: Honda
Colour: Yellow
Wheels: 4
Vehicle object created.
Make: Honda
Colour: Yellow
Wheels: 4


我编写的代码在这里,但不能编译(临时解决方案为灰色,但不符合测试标准,因为更改了main方法以从实例传递参数):

public class Vehicle {
// TODO your code goes here
// declare instance fields here!
String make;
String color;
int wheels;

public String aboutMe() {
    String statement = "Make: " + make + "\n" + "Colour: " + color + "\n" + "Wheels: " + wheels;
    return statement;
}

// constructor method
public Vehicle(String vehicleMake, String vehicleColor, int vehicleWheels) {
    make = vehicleMake;
    color = vehicleColor;
    wheels = vehicleWheels;
}
//Test:
public static void main(String[] args) {
    int wheels = 4;
    String make = "Honda";
    String color = "Yellow";
    System.out.println("Vehicle object created.");
    //CODE REQUIRED BY TEST:
    Vehicle v1 = new Vehicle(make);
    //Vehicle vh1 = new Vehicle("Honda", "Blue", 4);
    System.out.println(vh1.aboutMe());
    //CODE REQUIRED BY TEST:
    Vehicle v2 = new Vehicle(make, color);
    //Vehicle vh2 = new Vehicle("Honda", "Yellow", 4);
    System.out.println(vh2.aboutMe());
    //CODE REQUIRED BY TEST:
    Vehicle v3 = new Vehicle(make, color, wheels);
    //Vehicle vh3 = new Vehicle("Honda", "Yellow", 4);
    System.out.println(vh3.aboutMe());
}
//Code stops here
}


总之,如何将main中声明的实例传递给构造函数?

谢谢,这是我在这里的第一篇文章,非常感谢与我的合作,同时我了解了在此处提问的正确协议。

编辑:谢谢,代码现在运行非常感谢

public class Vehicle {
// TODO your code goes here
// declare instance fields here!

String make;
String color;
int wheels;

public String aboutMe() {
    String statement = "Vehicle object created.\n" + "  Make: " + make + "\n" + "  Colour: " + color + "\n" + "  Wheels: " + wheels;
    return statement;
}

//constructor method
public Vehicle(String vehicleMake, String vehicleColor, int vehicleWheels) {
    make = vehicleMake;
    color = vehicleColor;
    wheels = vehicleWheels;
}

public Vehicle(String make) {
    this(make, "Blue");
}

public Vehicle(String make, String color) {
    this(make, color, 4);
}

public static void main(String[] args) {
    int wheels = 4;
    String make = "Honda";
    String color = "Yellow";
    //System.out.println("Vehicle object created.");
    Vehicle v1 = new Vehicle(make);
    System.out.println(v1.aboutMe());
    Vehicle v2 = new Vehicle(make, color);
    System.out.println(v2.aboutMe());
    Vehicle v3 = new Vehicle(make, color, wheels);
    System.out.println(v3.aboutMe());
}
//    Code stops here
}

最佳答案

您需要创建其他构造函数

public Vehicle(String make) {
    this(make, "Blue");
}

public Vehicle(String make, String color) {
    this(make, color, 4);
}

07-26 02:52