继承创建对象是 判断指向父类还是子类

class Animal {
public void move() {
System.out.println("动物可以移动");
}
}

class Dog extends Demo {
public void move() {
System.out.println("狗可以跑和走");
}

public void bark() {
System.out.println("狗可以吠叫");
}
}

public class Demo {
public static void main(String args[]) {
Animal a = new Animal(); // Animal 对象
Animal b = new Dog(); // Dog 对象

a.move();// 执行 Animal 类的方法
b.move();// 执行 Dog 类的方法
b.bark();
}
}

//此时会报错

猜你喜欢

转载自www.cnblogs.com/dianzan/p/9663456.html
今日推荐