首页 青云排行榜 知识中心 控制台

下面程序结果为:(     )

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。
关于我们
公司简介
联系我们
联系我们
售前咨询: leizhongnan@eval100.com
售后服务: 0755-26415932
商务合作: support@eval100.com
友情链接
金蝶软件
快递100
关注我们
Copyright © 2023-2023 深圳慧题科技有限公司 粤ICP备2023109746号-1 粤公网安备44030002001082