Java:当B扩展A时,A x = new A()和A x = new B()之间的差异 [英] Java: difference between A x = new A() and A x = new B() when B extends A

查看:632
本文介绍了Java:当B扩展A时,A x = new A()和A x = new B()之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

java继承 - 请解释

我正在学习Java和我有两个问题:

I'm learning Java and I have two questions:


  1. 有什么区别:

  1. What is the difference between:

A x = new A();

A x = new B();

考虑到:

class A
class B extends A


  • 什么是之间的区别:

  • What's the difference between:

    A x = new B();
    (A)x.run_function();
    

    假设A和B都有函数 run_function ,哪一个会被执行?

    Let's say that both A and B have the function run_function, which one will be executed ?


    推荐答案

    最重要的区别在于静态和动态类型的对象以及对象的引用。

    The most important difference is between the static and dynamic types of objects and references to objects.

    说B扩展A和C扩展B。

    Say B extends A and C extends B.

    对象的动态类型(新的类型)是它的实际运行时类型:它定义了对象的实际方法。

    The dynamic type of an object (the type used in the new) is its actual runtime type: it defines the actual methods that are present for an object.

    对象引用的静态类型(变量)是编译时类型:它定义或者更确切地声明可以在变量引用的对象上调用哪些方法。

    The static type of an object reference (a variable) is a compile-time type: it defines, or rather declares, which methods can be called on the object the variable references.

    变量的静态类型应该始终是它所引用的对象的动态类型的相同类型或超类型。

    The static type of a variable should always be of the same type or a supertype of the dynamic type of the object it references.

    所以在我们的例子中,具有静态类型A的变量可以引用具有动态类型A,B和C的对象。
    变量能够使用静态类型B可以引用具有动态类型B和C的对象。
    具有静态类型C的变量只能引用具有动态类型C的对象。

    So in our example, a variable with static type A can reference objects with dynamic types A, B and C. A variable with static type B can reference objects with dynamic types B and C. A variable with static type C can only reference objects with dynamic type C.

    最后,在对象引用上调用方法是静态和动态类型之间的微妙而复杂的交互。 (如果你不相信我,请阅读方法调用的Java语言规范。)

    Finally, calling a method on an object reference is a subtle and complex interaction between static and dynamic types. (Read the Java Language Spec on method invocation if you don't believe me.)

    如果A和B都实现了方法f(),例如静态type是A,涉及的动态类型是C,用于方法调用,然后调用Bf():

    If both A and B implement a method f() for example, and the static type is A and the dynamic type involved is C for a method invocation, then B.f() will be invoked:

    B extends A, C extends B
    public A.f() {}
    public B.f() {}
    A x = new C(); // static type A, dynamic type C
    x.f(); // B.f() invoked
    

    大大简化:首先是两个接收器的静态类型(类型A)和arguments(no args)用于确定特定调用的最佳匹配(最具体)方法签名,这是在编译时完成的。这里显然是A.f()。

    Simplifying greatly: first the static types of both receiver (type A) and arguments (no args) are used to decide the best-matching (most specific) method signature for that particular invocation, and this is done at compile-time. Here, this is clearly A.f().

    然后,在运行时的第二步,动态类型用于定位方法签名的实际实现。我们从类型C开始,但是我们没有找到f()的实现,所以我们向上移动到B,并且我们有一个匹配A.f()的签名的方法B.f()。所以调用B.f()。

    Then, in a second step at runtime, the dynamic type is used to locate the actual implementation of our method signature. We start with type C, but we don't find an implementation of f(), so we move up to B, and there we have a method B.f() that matches the signature of A.f(). So B.f() is invoked.

    在我们的例子中,我们说方法B.f()会覆盖方法A.f()。在类型层次结构中重写方法的机制称为子类型多态。

    In our example we say that method B.f() overrides method A.f(). The mechanism of overriding methods in a type hierarchy is called subtype polymorphism.

    这篇关于Java:当B扩展A时,A x = new A()和A x = new B()之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆