Given:

class Vehicle {

    int x;

    Vehicle() {
        this(10); // line n1
    }

    Vehicle(int x) {
        this.x = x;
    }
}

class Car extends Vehicle {

    int y;

    Car() {
        super();
        this(20); // line n2
    }

    Car(int y) {
        this.y = y;
    }

    public String toString() {
        return super.x + ":" + this.y;
    }
}

And given the code fragment:

Vehicle y = new Car();
System.out.println(y);

What is the result?

A.

10:20

B.

0:20

C. Compilation fails at line n1
D. Compilation fails at line n2

题解

第 6 行(line n1)用「this」叙述调用了 Vehicle 对象的另一个构造器,而 Vehicle 对象的确拥有「Vehicle(int x)」构造器,因此这是没问题的。

第 20 行(line n2)也用「this」叙述调用了 Car 对象的另一个构造器,但并非在构造器的第 1 行使用「this」叙述,而会造成编译错误。