浅谈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

标签:Java,方法,调用,优先级
0
投稿

猜你喜欢

  • Android开发注解排列组合出启动任务ksp

    2022-10-06 21:25:16
  • Java接口的作用_动力节点Java学院整理

    2021-12-23 20:46:14
  • C#支付宝新版支付请求接口调用

    2023-02-23 15:50:45
  • .NET单点登陆的实现方法及思路

    2023-06-10 10:13:53
  • RocketMq深入分析讲解两种削峰方式

    2023-04-04 01:38:47
  • android实现打地鼠游戏

    2023-09-25 08:45:59
  • IDEA 2020.3最新永久激活码(免费激活到 2099 年,亲测有效)

    2023-07-14 05:37:43
  • Java基础教程之数组的定义与使用

    2022-04-24 10:24:12
  • C#二进制序列化实例分析

    2022-09-21 01:43:38
  • spring boot executable jar/war 原理解析

    2022-10-13 18:30:09
  • Groovy动态语言使用教程简介

    2022-04-28 15:05:54
  • Android发送xml数据给服务器的方法

    2021-12-19 13:06:44
  • 解析Java的Spring框架的BeanPostProcessor发布处理器

    2021-11-21 17:16:13
  • Java集合类的组织结构和继承、实现关系详解

    2023-03-09 10:48:50
  • Java语言中Swing组件编程详解

    2023-04-19 04:08:22
  • Android 自绘控件

    2022-02-19 17:08:13
  • java读取xml配置参数代码实例

    2023-11-25 03:03:17
  • C# Bitmap图像处理(含增强对比度的三种方法)

    2023-11-01 02:33:53
  • Spring Cloud Gateway全局通用异常处理的实现

    2022-05-08 08:03:10
  • Java回调函数实例代码详解

    2023-11-23 18:22:26
  • asp之家 软件编程 m.aspxhome.com