Java 简明教程
Java - Dynamic Binding
绑定是一种在方法调用和方法实际实现之间创建链接的机制。根据 polymorphism concept in Java , object 可以有许多不同的形式。对象形式可以在编译时和运行时进行解析。
Binding is a mechanism creating link between method call and method actual implementation. As per the polymorphism concept in Java, object can have many different forms. Object forms can be resolved at compile time and run time.
Java Dynamic Binding
Dynamic binding 指的是在运行时解析方法调用和方法实现之间链接的过程(或者,是在运行时调用重写方法的过程)。Dynamic binding 也称为 run-time polymorphism 或 late binding。动态绑定使用对象来解析绑定。
Dynamic binding refers to the process in which linking between method call and method implementation is resolved at run time (or, a process of calling an overridden method at run time). Dynamic binding is also known as run-time polymorphism or late binding. Dynamic binding uses objects to resolve binding.
Characteristics of Java Dynamic Binding
-
Linking − Linking between method call and method implementation is resolved at run time.
-
Resolve mechanism − Dynamic binding uses object type to resolve binding.
-
Example − Method overriding is the example of Dynamic binding.
-
Type of Methods − Virtual methods use dynamic binding.
Example of Java Dynamic Binding
在此示例中,我们创建了两个类 Animal 和 Dog,其中 Dog 类扩展了 Animal 类。在 main() 方法中,我们使用 Animal 类引用,并为其分配一个 Dog 类的对象以检查动态绑定效果。
In this example, we’ve created two classes Animal and Dog where Dog class extends Animal class. In main() method, we’re using Animal class reference and assign it an object of Dog class to check Dynamic binding effect.
package com.tutorialspoint;
class Animal {
public void move() {
System.out.println("Animals can move");
}
}
class Dog extends Animal {
public void move() {
System.out.println("Dogs can walk and run");
}
}
public class Tester {
public static void main(String args[]) {
Animal a = new Animal(); // Animal reference and object
// Dynamic Binding
Animal b = new Dog(); // Animal reference but Dog object
a.move(); // runs the method in Animal class
b.move(); // runs the method in Dog class
}
}
Animals can move
Dogs can walk and run
在上面的示例中,尽管 b 是 Animal 的一种类型,但它运行 Dog 类中的 move 方法。这样做的原因是:在编译时,针对引用类型进行检查。然而,在运行时,JVM 找出对象类型,并运行属于该特定对象的该方法。
In the above example, you can see that even though b is a type of Animal it runs the move method in the Dog class. The reason for this is: In compile time, the check is made on the reference type. However, in the runtime, JVM figures out the object type and would run the method that belongs to that particular object.
因此,在上面的示例中,程序将正确编译,因为 Animal 类具有 move 方法。然后,在运行时,运行特定于该对象的方法。
Therefore, in the above example, the program will compile properly since Animal class has the method move. Then, at the runtime, it runs the method specific for that object.
Java Dynamic Binding: Using the super Keyword
在调用重写方法的超类版本时,使用 super 关键字,以便能够在使用动态绑定时利用父类方法。
When invoking a superclass version of an overridden method the super keyword is used so that we can utilize parent class method while using dynamic binding.
Example: Using the super Keyword
在此示例中,我们创建了两个类 Animal 和 Dog,其中 Dog 类扩展了 Animal 类。Dog 类重写了其超类 Animal 的 move 方法。但是,它使用 super 关键字调用父类 move() 方法,以使在调用子方法时,由于动态绑定,两个 move 方法都被调用。在 main() 方法中,我们使用 Animal 类引用,并为其分配一个 Dog 类的对象以检查动态绑定效果。
In this example, we’ve created two classes Animal and Dog where Dog class extends Animal class. Dog class overrides the move method of its super class Animal. But it calls parent move() method using super keyword so that both move methods are called when child method is called due to dynamic binding. In main() method, we’re using Animal class reference and assign it an object of Dog class to check Dynamic binding effect.
class Animal {
public void move() {
System.out.println("Animals can move");
}
}
class Dog extends Animal {
public void move() {
super.move(); // invokes the super class method
System.out.println("Dogs can walk and run");
}
}
public class TestDog {
public static void main(String args[]) {
Animal b = new Dog(); // Animal reference but Dog object
b.move(); // runs the method in Dog class
}
}
Animals can move
Dogs can walk and run