浅谈Java方法调用的优先级问题
作者:Joke誓言 发布时间:2023-07-01 13:40:08
实现Java多态性的时候,关于方法调用的优先级:
我们这样假设下,super(超类)、this(当前类对象)、show(方法)、object(对象),方法调用优先顺序: ①this.show(object)>②super.show(object)> ③this.show((super)object)>④super.show((super)object)
先看以下代码
class ParentCls {
public String show(ChildA obj){
return "Parent and ChildA";
}
public String show(ParentCls obj) {
return "Parent";
}
}
然后写一个子类ChildA ,继承ParentCls :
class ChildA extends ParentCls{
public String show(ChildA obj) {
return "ChildA";
};
public String show(ParentCls obj) {
return "ChildA and Parent";
};
}
写一个子类ChildB,继承ChildA :
class ChildB extends ChildA{
}
测试下
public static void main(String[] args) {
ParentCls p1 = new ParentCls();
ParentCls p2 = new ChildA();
ChildA a = new ChildA();
ChildB b = new ChildB();
System.out.println(p1.show(a));
System.out.println(b.show(a));
System.out.println(a.show(b));
System.out.println(p2.show(a));
}
输出
Parent and ChildA
ChildA
ChildA
ChildA
第一个输出,p1是ParentCls的实例,且类ParentCls中有show(ChildA obj)方法,直接执行该方法, ①有效;
第二个输出,b是ChildB 的实例,类ChildB 中没有show(ChildA obj)方法,①无效,再从ChildB 的父类ChildA查找,ChildA中刚好有show(ChildA obj)方法,②有效;
第三个输出,a是ChildA的实例,b是ChildB的实例,类ChildA中没有show(ChildB obj)方法,①无效,再从ChildA的父类ParentCls入手,ParentCls中也没有show(ChildB obj)方法,②无效,从ChildB的父类入手,(super)ChildB 即是ChildA,所以a.show(b)=>a.show(a),ChildA中刚好有show(ChildA obj)方法,③有效;
④就不作演示,根据①②③很容易得出结论;
第四个输出,体现Java多态性,符合①,但是p2是引用类ChildA的一个对象 ,ChildA 重写覆盖了ParentCls的show()方法,所以执行ChildA 的show()方法;
补充知识:Java中关于静态块,初始化快,构造函数的执行顺序
代码如下:
public class ParentDemo {
static {
System.out.println("this is ParentDemo static");
}
{
System.out.println("this is ParentDemo code block");
}
public ParentDemo() {
System.out.println("this is ParentDemo constructor");
}
}
public class SonDemo extends ParentDemo{
static {
System.out.println("this is SonDemo static");
}
{
System.out.println("this is SonDemo code block");
}
public SonDemo() {
System.out.println("this is SonDemo constructor");
}
}
public class TestMain {
public static void main(String[] args){
new SonDemo();
}
}
输出结果:
this is ParentDemo static
this is SonDemo static
this is ParentDemo code block
this is ParentDemo constructor
this is SonDemo code block
this is SonDemo constructor
由上可见,Java中 静态块中的代码在类加载时执行,子类继承父类。会按照继承的顺序先执行静态代码块。当实例化对象的时候,同理会按照继承的顺序依次执行如下代码:
代码块,构造函数,当父类的执行完以后,再执行子类。
来源:https://blog.csdn.net/u014063265/article/details/68063497


猜你喜欢
- 新版的IDEA为了防止 pom 更新时,MAVEN 自动导包时卡死的问题,取消了自动导包机制。但新增了导入按钮和快捷键。 问题id
- 本文实例为大家分享了java绘制五子棋棋盘的具体代码,供大家参考,具体内容如下源码:import javax.imageio.ImageIO
- 配置AOPAOP简介要介绍面向切面变成(Aspect-Oriented Programming,AOP),需要先考虑一个这样的场景:公司有一
- future机制是在通过线程去执行某个任务的时候,如果比较耗时,我们可以通过futureTask机制,异步返回,继续去执行其他的任务,在需要
- SpringBoot下载Excel文件文件损坏我把模板文件放在了resources目录下maven插件打包项目的时候,默认会压缩resour
- System.getProperty(user.dir)定位问题前言随着学习java web 的深入学习,为了巩固自己的学习成果,练习了一个
- 本文实例为大家分享了java实现二叉树遍历的具体代码,供大家参考,具体内容如下二叉树如下:遍历结果如下:以下是实现代码:package bi
- OleDbConnection,OracleConnection 或者SqlConnection这种连接,直接执行sql语句。现在的连接方式
- 谷歌在推出Android5.0的同时推出了一些新控件,Android5.0中最常用的新控件有下面5种。1. CardView(卡片视图)Ca
- spring cloud gateway获取请求的真实地址在使用spring cloud gateway的时候,路由一般配置为服务名例如 l
- Spring Security OAuth 默认提供OAuth2.0 的四大基本授权方式(authorization_code\implic
- 结构型设计模式创建型设计模式主要是为了解决创建对象的问题,而结构型设计模式则是为了解决已有对象的使用问题。适配器模式适配器模式比较好理解,因
- 1.什么是并行计算传统并行计算:共享同一个数据,通过锁来控制数据的读写,难度大,容易导致死锁,拓展性差。但是是实时的,细颗粒度计算,计算密集
- 本文实例为大家分享了android实现录屏小功能的具体代码,供大家参考,具体内容如下思路android实现录屏功能有两种方案,一种是直接使用
- 一、Synchronized的基本使用Synchronized是Java中解决并发问题的一种最常用的方法,也是最简单的一种方法。Synchr
- 最近项目上要实现语音搜索功能,界面样式要模仿一下UC浏览器的样式,UC浏览器中有一个控件,会随着声音大小浮动,然后寻思偷个懒,百
- Jackson 日期格式化技巧使用 Spring Boot 时,需要使用 Jackson 处理一些 Java Time API 类型的 JS
- @requestBody的作用及说明1、@requestBody注解常用来处理content-type不是默认的application/x-
- 当一个产品或者项目由大量独立模块组成时,想要从 Git 挨个下载下来导入 IDE 查看并不容易,此时可以结合使用 Git 和 Maven 的
- 目录一、Ehcache缓存简介Hibernate缓存EhCache缓存特点对比Redis缓存二、集成SpringBoot框架1、核心依赖2、