Java——静态类型 实际类型
2024-09-03 19:41:37
public class test {
static class father {
void run() {
System.out.println("father run");
}
}
static class son extends father{
void run() {
System.out.println("son run");
}
} public void sayHello(son son) {
System.out.println("son");
} public void sayHello(father father) {
System.out.println("father");
} public static void main(String[] args) {
father a = new son();
a.run();
test t = new test();
t.sayHello(a);
System.out.println(a.getClass());
}
}
输出结果:
son run
father
class old.test$son
解释:
father a = new son()
这里面 father 是静态类型,son是实际类型。
静态类型是在编译期可知的,而实际类型是在运行期才可以知道,
所以当运行run()时,取的是子类方法,而将a作为参数传入时,是以father这个类型传入的。
这也说明了重写载是静态的,而重写是动态的。
最新文章
- 在centos6.5中安装jdk
- mvc+ef+oracle环境中报错:ORA-00001: 违反唯一约束条件
- dictionary ----- python
- kafka入门教程
- HDOJ 2736 Surprising Strings
- finally块执行时间
- UEditor上传图片到七牛C#(后端实现)
- 开源自己写的一个拖拽库,兼容到IE8+
- SQL注入攻击[详解]
- Who do you want to be bad? (谁会是坏人?)人工智能机器小爱的问话
- arcgis api 3.x for js 入门开发系列二十二地图模态层(附源码下载)
- stackExchange.redis 实现模糊匹配批量查询
- Redis之分布式锁
- POJ 3264 线段树入门解题报告
- Delphi IdTCPClient IdTCPServer 点对点传送文件
- S4 继承
- JavaEE之反射
- ToggleButton开关状态按钮控件
- PHPCMS分页原理
- oracle用户和权限
热门文章
- 双屏显示——NW.js
- 自定义Spring-Boot @Enable注解
- NOIP2013提高问题求解T2(关于递推与递归)
- python 生成器、三元表达式、列表推倒式、字典生成式、生成器表达式(总结)
- 交换机安全学习笔记 第五章 DHCP缺陷攻击
- MySQL-线上数据迁移实战记录
- memset函数及注意事项
- POJ 2995 Brackets 区间DP
- 解决 java.security.cert.CertificateException: java.lang.IllegalArgumentException: Invalid input to toASCII:
- css3 伪类以及伪元素的特效