我在VS代码的“调试控制台”中遇到此问题:
Exception in thread "main" java.lang.NoSuchMethodError: Car.<init>(Ljava/lang/String;)V
at CarTest2.main(CarTest2.java:23)
我正在看书中的一个例子,但这似乎行不通。
class Car {
String colour;
String gearType;
int door;
Car() {
this("white", "auto", 4);
}
Car(String colour) {
this(colour, "auto", 4);
}
Car(String colour, String gearType, int door) {
this.colour = colour;
this.gearType = gearType;
this.door = door;
}
}
class CarTest2 {
public static void main(String[] args) {
Car c1 = new Car();
Car c2 = new Car("blue");
System.out.println("c1.colour = " + c1.colour + ", c1.gearType = " + c1.gearType + ", c1.door = " + c1.door);
System.out.println("c2.colour = " + c2.colour + ", c2.gearType = " + c2.gearType + ", c2.door = " + c2.door);
}
}
预期答案:
c1.colour =白色,c1.gearType =“自动”,c1.door = 4 c2.colour =蓝色,c2.gearType =“自动”,c2.door = 4