【2021-07-05】分析程序的运行结果,并解释为什么?

未匹配的标注

请移步至::octocat:每日一题 查看更多的题目 ~

程序一:

public class Polymorphic {
    public static void main(String[] args) {
        Animal cat = new Cat();
        System.out.println(cat.name);
    }
}
class Animal {
    String name = "animal";
}
class Cat extends Animal{
    String name = "cat";
}

程序二:

public class Polymorphic {
    public static void main(String[] args) {
        Animal cat = new Cat();
        cat.speak();
    }
}
class Animal {
    public void speak(){
        System.out.println("我是一个动物");
    }
}
class Cat extends Animal{
    @Override
    public void speak() {
        System.out.println("我是一只猫");
    }
}

查看答案

本文章首发在 LearnKu.com 网站上。

上一篇 下一篇
讨论数量: 0
发起讨论 只看当前版本


暂无话题~