classCarextendsVehicle{publicCar(){System.out.println("AnewCar.");}publicvoiddrive(){System.out.println("Carisdriven");}publicstaticvoidbrake(){System.out.println("Carisb...
class Car extends Vehicle {
public Car() {
System.out.println("A new Car.");
}
public void drive() {
System.out.println("Car is driven");
}
public static void brake() {
System.out.println("Car is braked");
}
}
public class Vehicle {
class Bus extends Vehicle {
public Bus() {
System.out.println("A new Bus.");
}
public void drive() {
System.out.println("Bus is driven");
}
}
public Vehicle() {
System.out.println("A new Vehicle.");
}
public void drive() {
System.out.println("Vehicle is driven");
}
public static void brake() {
System.out.println("Vehicle is braked");
}
对上边的这个程序, 运行下边两个语句:
Vehicle vc = new Car();
Bus vb = new Bus();
Bus vb = new Bus();的结果是
A new Vehicle.
A new Bus.
这个还可以理解。。但是为什么 Vehicle vc = new Car();的结果是
A new Vehicle.
A new Vehicle.
A new Car.
?
展开