java中,子类继承父类之后,覆盖了原有方法,那么调用新的方法的时候,二者都调用吗?
classCarextendsVehicle{publicCar(){System.out.println("AnewCar.");}publicvoiddrive(){...
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.
? 展开
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.
? 展开
2个回答
展开全部
把Bus类定义到Vehicle类外面,这样运行就对了。
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");
}
}
class Bus extends Vehicle {
public Bus() {
System.out.println("A new Bus.");
}
public void drive() {
System.out.println("Bus is driven");
}
}
class Vehicle {
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");}
}
class Test{
public static void main(String[] args){
Vehicle vc = new Car();
Bus vb = new Bus();
}
}
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");
}
}
class Bus extends Vehicle {
public Bus() {
System.out.println("A new Bus.");
}
public void drive() {
System.out.println("Bus is driven");
}
}
class Vehicle {
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");}
}
class Test{
public static void main(String[] args){
Vehicle vc = new Car();
Bus vb = new Bus();
}
}
追问
bus那个放在里边也可以编译通过的说。。话说放在里边和外边有什么不同呀?觉得放在里边怪怪的。。。不过也没报错…………
又话说为什么Vehicle vc = new Car()可以编译通过呢?是不是所有的父类子类都可以这样定义?
追答
Vehicle vc = new Car()这个是java的多态,这样定义当然对了。所有的父类子类都可以。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询