首页 > Java > java教程 > 正文

Java 中的运行时多态性

王林
发布: 2024-08-30 15:43:45
原创
493 人浏览过

在本文中,我们将学习 Java 中的运行时多态性。 “Poly”的意思是“许多”,“morph”的意思是“类型”。因此,术语“多态性”表示同一事物的不同类型。这里我们将看到Java如何在运行时归档多态性,这意味着,在编译之后但在代码运行之前。

语法:

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

对于Java中的运行时多态性,您应该遵循带有注释的java基本语法。

@Override
登录后复制

这里可以使用注释来指出我们要具体覆盖哪个方法。

运行时多态性在 Java 中如何工作?

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

为什么叫运行时多态?

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

它也称为“动态方法调度”。之所以如此命名,是因为该方法的功能是在运行时由 JVM 根据对象动态决定的

也称为“后期绑定”,因为方法和对象的绑定,即显示哪个对象的方法的功能,是后期决定的,即在编译之后。

规则 Runtime 多态性

的限制

以下是运行时多态性的一些规则和限制:

运行时多态规则

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

运行时多态性的局限性

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

Java 中运行时多态性的示例

 我们将在这里讨论一些运行时多态性的代码示例。

示例#1

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

代码:

class Parents {
public void showcase () {
System.out.println("I am Parent");
}
}
class Children extends Parents {
@Override
public void showcase () {
System.out.println("I am Children");
}
}
public class RunTimePolymorphism {
public static void main(String args[]) {
Parents superObject = new Parents();
superObject.showcase(); //method of super class or parent class is called
Parents subObject = new Children(); // upcasting
subObject.showcase();//method of sub class or child class is called by Parent reference, this is called "Run time Polymorphism"
Children subObject2 = new Children();
subObject2.showcase(); //method of sub class or child class is called
}
}
登录后复制

输出:

Java 中的运行时多态性

示例#2

让我们举一个多级继承情况下的运行时多态性的例子。在此示例中,我们考虑了两个继承级别。在此示例中,我们将展示方法 sip() 如何根据与其关联的对象类型显示不同的消息。当它与“人类”类型关联时,它显示来自父类的消息。当它与“Man”类型关联时,它显示来自其子类的消息。同样,在继承的第二级中,当与“Baby”类型关联时,它显示来自其父类“Man”类的子类的消息。

代码:

class Human{
void sip() {
System.out.println("Human is sipping");
}
}
class Man extends Human{
void sip(){
System.out.println("Man is sipping soup");
}
}
class Baby extends Man{
void sip(){
System.out.println("Baby is sipping milk");
}
}
public class RunTimePolymorphism {
public static void main(String args[]){
Human superObject=new Human();
Human subObject=new Man();  // // upcasting : first level of heritance
Human babyObject=new Baby();  // // upcasting : second level of heritance
superObject.sip();
subObject.sip();  //run time polymorphism happening in first level of heritance
babyObject.sip(); //run time polymorphism happening in second level of heritance
}
}
登录后复制

输出:

Java 中的运行时多态性

示例#3

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

代码:

class OperatingSytem{
void feature() {
System.out.println("This is Operating Sytem");
}
}
class DOS extends OperatingSytem{
void feature(){
System.out.println("This is DOS");
}
}
class Windows extends DOS{
void feature(){
System.out.println("This is Windows");
}
}
class WindowsMobile extends Windows{
void feature(){
System.out.println("This is Windows Mobile");
}
}
public class RunTimePolymorphism {
public static void main(String args[]){
OperatingSytem superObject=new OperatingSytem();
OperatingSytem subObject=new DOS();  // child object type : first level of heritance
OperatingSytem sub2Object=new Windows();  // child object type : second level of heritance
OperatingSytem sub3Object=new WindowsMobile();  // 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
}
}
登录后复制

Output:

Java 中的运行时多态性

Conclusion

This concludes our learning of the topic “Runtime Polymorphism in Java”. Write yourself the codes mentioned in the above examples in the java compiler and verify the output. Learning of codes will be incomplete if you will not write code by yourself.

以上是Java 中的运行时多态性的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!