在本文中,我们将学习 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 } }
输出:
示例#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 } }
输出:
示例#3
让我们再举一个多级继承情况下运行时多态性的例子。在此示例中,我们考虑了三个继承级别。在此示例中,我们将展示方法 feature () 如何根据与其关联的对象类型显示不同的功能。当它与“操作系统”类型关联时,它显示来自父类的消息。当它与“DOS”类型关联时,它显示来自其子类的消息。同样,在继承的第二级中,当与“Windows”类型关联时,它显示来自其父类“DOS”类的子类的消息。同样,在第三级继承中,当与“WindowsMobile”类型关联时,它显示来自其父级“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:
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中文网其他相关文章!

JVM'SperformanceIsCompetitiveWithOtherRuntimes,operingabalanceOfspeed,安全性和生产性。1)JVMUSESJITCOMPILATIONFORDYNAMICOPTIMIZAIZATIONS.2)c提供NativePernativePerformanceButlanceButlactsjvm'ssafetyFeatures.3)

JavaachievesPlatFormIndependencEthroughTheJavavIrtualMachine(JVM),允许CodeTorunonAnyPlatFormWithAjvm.1)codeisscompiledIntobytecode,notmachine-specificodificcode.2)bytecodeisisteredbytheybytheybytheybythejvm,enablingcross-platerssectectectectectross-eenablingcrossectectectectectection.2)

TheJVMisanabstractcomputingmachinecrucialforrunningJavaprogramsduetoitsplatform-independentarchitecture.Itincludes:1)ClassLoaderforloadingclasses,2)RuntimeDataAreafordatastorage,3)ExecutionEnginewithInterpreter,JITCompiler,andGarbageCollectorforbytec

JVMhasacloserelationshipwiththeOSasittranslatesJavabytecodeintomachine-specificinstructions,managesmemory,andhandlesgarbagecollection.ThisrelationshipallowsJavatorunonvariousOSenvironments,butitalsopresentschallengeslikedifferentJVMbehaviorsandOS-spe

Java实现“一次编写,到处运行”通过编译成字节码并在Java虚拟机(JVM)上运行。1)编写Java代码并编译成字节码。2)字节码在任何安装了JVM的平台上运行。3)使用Java原生接口(JNI)处理平台特定功能。尽管存在挑战,如JVM一致性和平台特定库的使用,但WORA大大提高了开发效率和部署灵活性。

JavaachievesPlatFormIndependencethroughTheJavavIrtualMachine(JVM),允许Codetorunondifferentoperatingsystemsswithoutmodification.thejvmcompilesjavacodeintoplatform-interploplatform-interpectentbybyteentbytybyteentbybytecode,whatittheninternterninterpretsandectectececutesoneonthepecificos,atrafficteyos,Afferctinginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginging

JavaispoperfulduetoitsplatFormitiondence,对象与偏见,RichstandardLibrary,PerformanceCapabilities和StrongsecurityFeatures.1)Platform-dimplighandependectionceallowsenceallowsenceallowsenceallowsencationSapplicationStornanyDevicesupportingJava.2)

Java的顶级功能包括:1)面向对象编程,支持多态性,提升代码的灵活性和可维护性;2)异常处理机制,通过try-catch-finally块提高代码的鲁棒性;3)垃圾回收,简化内存管理;4)泛型,增强类型安全性;5)ambda表达式和函数式编程,使代码更简洁和表达性强;6)丰富的标准库,提供优化过的数据结构和算法。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver Mac版
视觉化网页开发工具

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

Dreamweaver CS6
视觉化网页开发工具