【问题标题】:What is the meaning of the type of reference [duplicate]引用类型的含义是什么[重复]
【发布时间】:2017-11-25 18:39:11
【问题描述】:
class Parent {
    int m;
}

class child extends Parent {
    public static void main(String args[]) {
        Parent x = new Child(); 
    }
}

x is of type Parent 是什么意思?我们为什么要利用 这样的引用虽然我们可以使用Child x = new Child()?

【问题讨论】:

  • 因缺乏先前的研究而被否决。

标签: java dynamic reference reference-type dynamic-binding


【解决方案1】:

List<Shape> 可以包含矩形、圆形,如果它们是 Shape 的子类型。 这是一个很好的例子。

【讨论】:

  • 阅读polymorphism了解更多示例。
  • 我可以使用列表来添加内容。如果我有 Parent 类型引用,我该怎么办?我无法添加任何内容。
【解决方案2】:

Java 动态解析对象的类。这意味着对象的实际类型在指示的类型上被解析。

因此,在您的示例中,如果Child 包含来自Parent 的重载方法,则该方法将在从Parent 继承的方法之前首先被调用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-14
    • 2013-12-31
    • 1970-01-01
    • 2021-07-19
    • 2013-04-26
    • 2014-03-22
    • 1970-01-01
    • 2015-12-26
    相关资源
    最近更新 更多