
一文搞懂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会调用引用所指对象实际类型的相应方法。
发表评论
最新留言
感谢大佬
[***.8.128.20]2025年04月07日 17时51分40秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
elementUi源码解析(1)--项目结构篇
2021-05-09
自动遍历测试之Monkey工具
2021-05-09
Nmap扫描工具介绍
2021-05-09
算法笔记:递归、动态规划
2021-05-09
Pytest插件开发
2021-05-09
常用Windows 快捷键
2021-05-09
linux命令-压缩与打包
2021-05-09
ORACLE 11g 生产中高水位线(HWM)处理
2021-05-09
centos 6.x 编译安装 pgsql 9.6
2021-05-09
weblogic 服务器部署SSL证书
2021-05-09
oracle 11g not in 与not exists 那个高效?
2021-05-09
Linux 安装Redis 5.0(以及参数调优)
2021-05-09
html5 Game开发系列文章之 零[开篇]
2021-05-09
为什么阿里巴巴建议集合初始化时,指定集合容量大小
2021-05-09
为什么阿里巴巴要求谨慎使用ArrayList中的subList方法
2021-05-09
Redis不是一直号称单线程效率也很高吗,为什么又采用多线程了?
2021-05-09
基于Python的Appium环境搭建合集
2021-05-09
Requests实践详解
2021-05-09