public class Test extends Father{
    private String name = "test";
    public static void main(String[] args){
        Test test = new Test();
        System.out.println(test.getName());
    }
}
class Father{
    private String name="father";
    public String getName(){
        return name;
    }        
}

问题: 以上程序的运行结果是:

选项: A father

    B test

    C 编译出错

    D 运行出错,无输出

解析:同理有子类父类继承关系,在子类对象中想调用从父类继承的getName()方法,方法是public的肯定没问题,

   关键在于return的name,是哪个name,是 父类中的name,还是子类中的name呢,当然是父类中的那个name了,你调用的是父类的方法,那当然返回的是父类的属性

   因此此题选father。

相关文章:

  • 2021-08-15
  • 2022-01-13
  • 2021-07-02
  • 2021-11-27
  • 2022-12-23
  • 2021-08-29
  • 2021-11-17
  • 2021-12-29
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-01-02
  • 2021-08-16
  • 2021-10-03
  • 2021-11-20
  • 2022-12-23
相关资源
相似解决方案