简体   繁体   English

Java中的动态绑定?

[英]Dynamic binding in java?

I have a little confusion with Dynamic Binding in java. 我对Java中的动态绑定有些困惑。 Here is a program, I want to know that, is there dynamic binding occurs or something else. 这是一个程序,我想知道,是否发生了动态绑定或其他情况。 What dynamic binding actually is? 什么是动态绑定?

class A {
int a;

public A() {
    a = 9;
}

public void show() {
    System.out.print("show in A ; ");
    System.out.println("a : " + a);
}
}
public class B extends A {
public B() {
    a = 8;
}

public void show() {
    System.out.print("show in B ; ");
    System.out.println("a : " + a);
}

public static void main(String[] args) {
    B p = new B();
    p.show();

    A q = new B();
    q.show();
}
}

It's here 它在这里

 A q = new B();
 q.show();

Compiler uses virtual call instructions ( invokeVirtual or invokeInterface ) for methods which can be overriden (they cannot be static or private). 编译器将虚拟调用指令( invokeVirtualinvokeInterface )用于可以被覆盖的方法(它们不能是静态的或私有的)。 In this code JVM detects that A.show is virtual and checks the actual type of q . 在此代码中,JVM检测到A.show是虚拟的,并检查q的实际类型。 Since it is B it calls B.show . 由于它是B因此称为B.show If it were static JVM would call A.show and we would see 如果是静态的,JVM将调用A.show ,我们将看到

show in A

I guess I confused you previously. 我想我以前很困惑。

Dynamic (or late) binding is how polymorphism is implemented in Java. 动态(或后期)绑定是在Java中实现多态的方式。 It occurs any time an instance method is invoked. 每当调用实例方法时就会发生这种情况。

In your example, we are interested in the occurrences here 在您的示例中,我们对这里的事件感兴趣

p.show();

and here 和这里

q.show();

At compilation time, the static type of the variable will be checked to see if the method show() is accessible, failing if it is not. 在编译时,将检查变量的静态类型,以查看方法show()是否可访问,否则无法访问。

At runtime (dynamic), the run time (dynamic) type of the object will be checked to find an implementation of the method. 在运行时(动态),将检查对象的运行时(动态)类型以找到该方法的实现。 If one is found, it is used, if not, the JVM keeps looking up the inheritance hierarchy. 如果找到一个,则使用它,否则,JVM继续查找继承层次结构。

For example 例如

A q = new B();
q.show();

at run time, q is of type B and B overrides show() so B#show() is invoked. 在运行时, q的类型为B并且B覆盖show()因此调用B#show()


In your answer I had commented on, the overriden method did not play a part. 在您评论过的答案中,重写方法没有起作用。 It was more a question of constructor execution order. 更多的是构造函数执行顺序的问题。

The method show() invoked in the below lines 下面几行中调用的show()方法

A q = new B();
q.show();

The target object of the show method is determined at runtime. show方法的目标对象在运行时确定。

you can change it in your main method to implement polymorphism or dynamic binding: 您可以在main方法中对其进行更改以实现多态或动态绑定:

public static void main(String[] args) {
    A a = new A();       
    a.show();

    a = new B();       
    a.show();
}

as you can tell that even the reference is never changed A a but what it refer is changed, as different instance it point to, difference behavior will be done! 如您所知,即使引用永远都不会改变A a但是引用的内容也会改变,因为它指向的是不同的实例,所以行为会有所不同!
This is so called polymorphism . 这就是所谓的polymorphism

Much powerful polymorphism, after using reflect in java will indulge you! 强大的多态性,在Java中使用reflect后会沉迷于您! have fun! 玩得开心!

- Binding is act of method call identifying and calling its method body . -绑定行为 method call identifying and calling its method body

- Java only supports Dynamic Binding, except few exception cases . -Java仅支持动态绑定, 少数例外情况除外

- The compiler can't understand which method call to be associated with which method body ... so its the responsibility of the method call to associated itself to its appropriate method body. - 编译器无法理解哪个method call与哪个method body相关联...因此,方法调用职责是将自身与相应的方法主体相关联。

- Its very important to know that Fields (Instance Variables) are NOT POLYMORPHIC IN NATURE. -重要的是要知道Fields (实例变量)在自然界不是多态的。

In your case the Dynamic binding is here: 您的情况是动态绑定在这里:

 A q = new B();
 q.show();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM