【问题标题】:About inheritance of java [duplicate]关于java的继承[重复]
【发布时间】:2016-10-15 09:04:48
【问题描述】:

/** * 张中正于 2016 年 10 月 15 日创建。 */

公共类扩展测试 {

static Dog d = new Dog();

public static void main(String[] args) {
    Animal a = d;
    System.out.println(a instanceof Animal);//true
    System.out.println(a instanceof Dog);//true

    System.out.println(d instanceof Animal);//true
    System.out.println(d instanceof Dog);//true
}

static class Animal {

}

static class Dog extends Animal {

}

}

为什么都是真的??????

【问题讨论】:

  • 这里有三个应该很明显,如果你花一点时间调查instanceof或者只是慢慢考虑这个名字,另一个也是。

标签: java


【解决方案1】:

这里的动物是父母,狗是孩子。

父类引用可以包含子类对象。

Animal 动物 = new Dog();

所以以上条件都成立。

【讨论】:

    【解决方案2】:

    根据类型的概念,instanceos 说,“你是这个类,还是从这个类派生的类?”--------

    【讨论】:

      猜你喜欢
      • 2015-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-21
      • 2014-08-06
      • 2017-09-20
      • 1970-01-01
      相关资源
      最近更新 更多