Java:在定义对象类型时使用超类与类之间有什么区别?

时间:2019-02-07 03:20:51

标签: java class reference subclass superclass

这里的变量ab的对象的行为是否有区别?

class Animal {
    public void makeSound() {
        System.out.println("Grr...");
    }
}

class Dog extends Animal {
    public void makeSound() {
        System.out.println("Woof");
    }
}

class Program {
    public static void main(String args[ ]) {
        Animal a = new Dog();
        Dog b = new Dog();

        a.makeSound();
        b.makeSound();
    }
}

0 个答案:

没有答案