淺談Java方法調(diào)用的優(yōu)先級問題
實現(xiàn)Java多態(tài)性的時候,關(guān)于方法調(diào)用的優(yōu)先級:
我們這樣假設(shè)下,super(超類)、this(當(dāng)前類對象)、show(方法)、object(對象),方法調(diào)用優(yōu)先順序: ①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)方法,直接執(zhí)行該方法, ①有效;
第二個輸出,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)方法,③有效;
④就不作演示,根據(jù)①②③很容易得出結(jié)論;
第四個輸出,體現(xiàn)Java多態(tài)性,符合①,但是p2是引用類ChildA的一個對象 ,ChildA 重寫覆蓋了ParentCls的show()方法,所以執(zhí)行ChildA 的show()方法;
補充知識:Java中關(guān)于靜態(tài)塊,初始化快,構(gòu)造函數(shù)的執(zhí)行順序
代碼如下:
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();
}
}
輸出結(jié)果:
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中 靜態(tài)塊中的代碼在類加載時執(zhí)行,子類繼承父類。會按照繼承的順序先執(zhí)行靜態(tài)代碼塊。當(dāng)實例化對象的時候,同理會按照繼承的順序依次執(zhí)行如下代碼:
代碼塊,構(gòu)造函數(shù),當(dāng)父類的執(zhí)行完以后,再執(zhí)行子類。
以上這篇淺談Java方法調(diào)用的優(yōu)先級問題就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
springboot CompletableFuture異步線程池詳解
這篇文章主要介紹了springboot CompletableFuture異步線程池的使用,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-04-04
Java引用傳遞實現(xiàn)方式以及與值傳遞的區(qū)別
這篇文章主要給大家介紹了關(guān)于Java引用傳遞實現(xiàn)方式以及與值傳遞的區(qū)別的相關(guān)資料,引用傳遞指在調(diào)用函數(shù)時將實際參數(shù)的地址直接傳遞到函數(shù)中,那么在函數(shù)中對參數(shù)所進行的修改,將影響到實際參數(shù),需要的朋友可以參考下2023-09-09
SpringBoot3.0集成MybatisPlus的實現(xiàn)方法
本文主要介紹了SpringBoot3.0集成MybatisPlus的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-08-08
Spring Boot 2 整合 QuartJob 實現(xiàn)定時器實時管理功能
Quartz是一個完全由java編寫的開源作業(yè)調(diào)度框架,形式簡易,功能強大。接下來通過本文給大家分享Spring Boot 2 整合 QuartJob 實現(xiàn)定時器實時管理功能,感興趣的朋友一起看看吧2019-11-11
java如何確定一個鏈表有環(huán)及入口節(jié)點
這篇文章主要介紹了java如何確定一個鏈表有環(huán)及入口節(jié)點,想了解數(shù)據(jù)結(jié)構(gòu)的同學(xué)可以參考下2021-04-04

