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

查看:31
本文介绍了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(),比如一个方法调用的静态类型是A,动态类型是C,那么B.f()就会被调用:

    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)和参数(无参数)的静态类型用于确定该特定调用的最佳匹配(最具体)方法签名,这是在编译时完成的.在这里,这显然是 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天全站免登陆