淺談Java方法調(diào)用的優(yōu)先級(jí)問(wèn)題
實(shí)現(xiàn)Java多態(tài)性的時(shí)候,關(guān)于方法調(diào)用的優(yōu)先級(jí):
我們這樣假設(shè)下,super(超類(lèi))、this(當(dāng)前類(lèi)對(duì)象)、show(方法)、object(對(duì)象),方法調(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"; } }
然后寫(xiě)一個(gè)子類(lèi)ChildA ,繼承ParentCls :
class ChildA extends ParentCls{ public String show(ChildA obj) { return "ChildA"; }; public String show(ParentCls obj) { return "ChildA and Parent"; }; }
寫(xiě)一個(gè)子類(lèi)ChildB,繼承ChildA :
class ChildB extends ChildA{
}
測(cè)試下
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
第一個(gè)輸出,p1是ParentCls的實(shí)例,且類(lèi)ParentCls中有show(ChildA obj)方法,直接執(zhí)行該方法, ①有效;
第二個(gè)輸出,b是ChildB 的實(shí)例,類(lèi)ChildB 中沒(méi)有show(ChildA obj)方法,①無(wú)效,再?gòu)腃hildB 的父類(lèi)ChildA查找,ChildA中剛好有show(ChildA obj)方法,②有效;
第三個(gè)輸出,a是ChildA的實(shí)例,b是ChildB的實(shí)例,類(lèi)ChildA中沒(méi)有show(ChildB obj)方法,①無(wú)效,再?gòu)腃hildA的父類(lèi)ParentCls入手,ParentCls中也沒(méi)有show(ChildB obj)方法,②無(wú)效,從ChildB的父類(lèi)入手,(super)ChildB 即是ChildA,所以a.show(b)=>a.show(a),ChildA中剛好有show(ChildA obj)方法,③有效;
④就不作演示,根據(jù)①②③很容易得出結(jié)論;
第四個(gè)輸出,體現(xiàn)Java多態(tài)性,符合①,但是p2是引用類(lèi)ChildA的一個(gè)對(duì)象 ,ChildA 重寫(xiě)覆蓋了ParentCls的show()方法,所以執(zhí)行ChildA 的show()方法;
補(bǔ)充知識(shí):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
由上可見(jiàn),Java中 靜態(tài)塊中的代碼在類(lèi)加載時(shí)執(zhí)行,子類(lèi)繼承父類(lèi)。會(huì)按照繼承的順序先執(zhí)行靜態(tài)代碼塊。當(dāng)實(shí)例化對(duì)象的時(shí)候,同理會(huì)按照繼承的順序依次執(zhí)行如下代碼:
代碼塊,構(gòu)造函數(shù),當(dāng)父類(lèi)的執(zhí)行完以后,再執(zhí)行子類(lèi)。
以上這篇淺談Java方法調(diào)用的優(yōu)先級(jí)問(wèn)題就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
創(chuàng)建并運(yùn)行一個(gè)java線程方法介紹
這篇文章主要介紹了創(chuàng)建并運(yùn)行一個(gè)java線程,涉及線程代碼示例,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11Java中JVM的雙親委派、內(nèi)存溢出、垃圾回收和調(diào)優(yōu)詳解
這篇文章主要介紹了Java中JVM的雙親委派、內(nèi)存溢出、垃圾回收和調(diào)優(yōu)詳解,類(lèi)加載器是Java虛擬機(jī)(JVM)的一個(gè)重要組成部分,它的主要作用是將類(lèi)的字節(jié)碼加載到內(nèi)存中,并生成對(duì)應(yīng)的Class對(duì)象,需要的朋友可以參考下2023-07-07值得收藏!教你如何在IDEA中快速查看Java字節(jié)碼
開(kāi)發(fā)中如果我們想看JVM虛擬機(jī)怎么編譯我們的Java文件,生成字節(jié)碼的,用IDEA工具就可以查看,本篇文章就給大家詳細(xì)介紹,對(duì)正在學(xué)習(xí)java的小伙伴們很有幫助,需要的朋友可以參考下2021-05-05mybatis中bind標(biāo)簽和concat的使用說(shuō)明
這篇文章主要介紹了mybatis中bind標(biāo)簽和concat的使用說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12Spring中的NamespaceHandler接口及相關(guān)軟件包說(shuō)明
這篇文章主要介紹了Spring中的NamespaceHandler接口及相關(guān)軟件包說(shuō)明,NamespaceHandler 接口,DefaultBeanDefinitionDocumentReader 使用該接口來(lái)處理在spring xml 配置文件中自定義的命名空間,需要的朋友可以參考下2023-12-12java實(shí)現(xiàn)簡(jiǎn)單貪吃蛇小游戲
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡(jiǎn)單貪吃蛇小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05Springboot整合JPA配置多數(shù)據(jù)源流程詳解
這篇文章主要介紹了Springboot整合JPA配置多數(shù)據(jù)源,JPA可以通過(guò)實(shí)體類(lèi)生成數(shù)據(jù)庫(kù)的表,同時(shí)自帶很多增刪改查方法,大部分sql語(yǔ)句不需要我們自己寫(xiě),配置完成后直接調(diào)用方法即可,很方便2022-11-11