下面程序结果为:( )
class Bird {
void talk() {
System.out.print("chirp ");
}
}
class Parrot2 extends Bird {
protected void talk() {
System.out.print("hello ");
}
public static void main(String[] args) {
Bird[] birds = {new Bird(), new Parrot2()};
for (Bird b : birds)
b.talk();
}
}chirp chirp
hello hello
chirp hello
编译错误
方法重写:
我们将介绍在 Java 中,当设计类时,被重写的方法的行为怎样影响多态性。
我们已经讨论了方法的重写,也就是子类能够重写父类的方法。
当子类对象调用重写的方法时,调用的是子类的方法,而不是父类中被重写的方法。
要想调用父类中被重写的方法,则必须使用关键字 super。
我们将介绍在 Java 中,当设计类时,被重写的方法的行为怎样影响多态性。
我们已经讨论了方法的重写,也就是子类能够重写父类的方法。
当子类对象调用重写的方法时,调用的是子类的方法,而不是父类中被重写的方法。
要想调用父类中被重写的方法,则必须使用关键字 super。