一文搞懂Java的多态性
发布日期:2021-05-08 06:01:25 浏览次数:18 分类:精选文章

本文共 531 字,大约阅读时间需要 1 分钟。

Polymorphism is the ability of an  object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is uesed to refer to a child class object

1. 多态的例子

public interface Vegetarian {}public class Animal {}public class Deer extends Animal implements Vegetarian {}// a Deer IS-A Animal// a Deer IS-A Vegetarian// a Deer IS-A Deer// a Deer IS-A Object

2. 虚函数(virtual method)

Java并没有virtual关键字,其多态的实现依赖于运行时刻对象实际类型。

编译阶段,编译器会根据引用声明的类型进行类型检查。

运行阶段,一个引用很可能引用的是其子类的对象,因此运行时刻,JVM会调用引用所指对象实际类型的相应方法

上一篇:pytorch lightning使用(简要介绍)
下一篇:一文搞懂Java的方法重载规则

发表评论

最新留言

感谢大佬
[***.8.128.20]2025年04月07日 17时51分40秒