首页  >  文章  >  Java  >  Java 中的动态绑定

Java 中的动态绑定

PHPz
PHPz原创
2024-08-30 16:06:55801浏览

“动态”的意思是“运行时”,“绑定”的意思是“关联”。因此,术语“动态绑定”表示 Java 虚拟机在运行时关联对象。这里我们将看到Java如何在运行时实现动态绑定,即在代码最终运行之前、编译之后。

语法:对于 Java 中的动态绑定,您应该遵循带有注释的 java 基本语法。你可以在这里使用@Override注解来指出我们具体要重写哪个方法。

广告 该类别中的热门课程 JAVA 掌握 - 专业化 | 78 课程系列 | 15 次模拟测试

动态绑定在 Java 中如何工作?

运行时多态性在 Java 中通过方法重写来工作。当对象与其父类具有相同的方法名称、参数和类型但具有不同的功能时,就会发生方法重写。如果子类中有这种类型的方法,我们将其称为重写方法。

为什么叫动态绑定?

之所以如此命名,是因为该方法的功能是由 JVM 在运行时根据对象动态决定的。它也称为“运行时多态性”。当我们通过父类的类型引用调用子类的重写方法时(这种现象在java中称为“Upcasting”),则对象的类型指示将调用哪个方法或功能。这个决定是在代码编译后由 JVM 在运行时做出的。因此,它被称为运行时多态性。也称为“后期绑定”,因为方法和对象的绑定,即显示哪个对象的方法的功能,是后期决定的,即在编译之后。

有关动态绑定的规则

  • 子类和父类的方法或函数必须具有相同的名称。
  • 子类和父类的方法或函数必须具有相同的参数。
  • 继承关系是强制性的(IS-A关系)。

动态绑定的限制

  • 您不能覆盖父类的私有方法。
  • 你不能重写 Final 方法。
  • 您不能重写静态方法。

实现动态绑定的示例

我们将在这里讨论动态绑定的一些代码示例:

示例#1

在此示例中,我们将展示locate() 方法如何根据与其关联的对象类型显示不同的消息。当它与“大陆”类型关联时,它显示来自父类的消息。当它与“SubContinent”类型关联时,它会显示来自子类的消息。

代码:

class Continent {
public void locate () {
System.out.println("We are in Continent");
}
}
class SubContinent extends Continent {
@Override
public void locate () {
System.out.println("We are in SubContinent");
}
}
public class DynamicBinding {
public static void main(String args[]) {
Continent superObject = new Continent ();
superObject.locate(); //method of super class or parent class is called
SubContinent subObject = new SubContinent (); // upcasting
subObject.locate();//method of sub class or child class is called by Parent reference, this is called "Dynamic Binding"
SubContinent subObject2 = new SubContinent ();
subObject2.locate(); //method of sub class or child class is called
}
}

输出:

Java 中的动态绑定

示例#2

让我们以多级继承情况下的动态绑定为例。在此示例中,我们考虑了两个继承级别。在此示例中,我们将展示识别方法如何根据与其关联的对象类型显示不同的消息。当它与“计算机”类型关联时,它显示来自父类的消息。当它与“桌面”类型关联时,它显示来自其子类的消息。同样,在继承的第二级中,当与“笔记本电脑”类型关联时,它显示来自其父级“桌面”类的子类的消息。

代码:

class Computer {
void identify() {
System.out.println("This is Computer");
}
}
class Desktop extends Computer {
void identify (){
System.out.println("This is Desktop");
}
}
class Laptop extends Desktop {
void identify (){
System.out.println("This is Laptop");
}
}
public class DynamicBinding {
public static void main(String args[]){
Computer superObject=new Computer ();
Computer subObject=new Desktop (); // // upcasting : first level of heritance
Computer babyObject=new Laptop (); // // upcasting : second level of heritance
superObject.identify ();
subObject.identify (); //run time polymorphism happening in first level of heritance
babyObject.identify (); //run time polymorphism happening in second level of heritance
}
}

输出:

Java 中的动态绑定

示例 #3

让我们再举一个多级继承情况下运行时多态性的例子。在此示例中,我们考虑了三个继承级别。在此示例中,我们将展示方法 feature () 如何根据与其关联的对象类型显示不同的功能。当它与“化妆品”类型关联时,它显示来自父类的消息。当它与“Perfume”类型关联时,它会显示来自其子类的消息。同样,在继承的第二级中,当与“Deo”类型关联时,它显示来自其父类“Perfume”类的子类的消息。同样在第三级继承中,当与“DeoStick”类型关联时,它显示来自其父类“Deo”类的子类的消息。

代码:

class Cosmetics{
void feature() {
System.out.println("Cosmetics are expensive");
}
}
class Perfume extends Cosmetics {
void feature(){
System.out.println("Perfume is soothing");
}
}
class Deo extends Cosmetics {
void feature(){
System.out.println("Deo is sometimes better than perfume");
}
}
class DeoStick extends Deo{
void feature(){
System.out.println("DeoStick is very handy");
}
}
public class RunTimePolymorphism {
public static void main(String args[]){
Cosmetics superObject=new Cosmetics ();
Cosmetics subObject=new Perfume(); // child object type : first level of heritance
Cosmetics sub2Object=new Deo(); // child object type : second level of heritance
Cosmetics sub3Object=new DeoStick(); // child object type : third level of heritance
superObject.feature();
subObject.feature(); //run time polymorphism happening in first level of heritance
sub2Object.feature(); //run time polymorphism happening in second level of heritance
sub3Object.feature(); //run time polymorphism happening in third level of heritance
}
}

输出:

Java 中的动态绑定

结论

我们对“Java 中的动态绑定”主题的学习到此结束。在java编译器中自己编写上面示例中提到的代码并验证输出。如果你不会自己写代码,那么代码的学习是不完整的。

以上是Java 中的动态绑定的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
上一篇:Applets in Java下一篇:Java Parse String