午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

淺談Java方法調(diào)用的優(yōu)先級(jí)問(wèn)題

瀏覽:20日期:2022-08-23 13:13:34

實(shí)現(xiàn)Java多態(tài)性的時(shí)候,關(guān)于方法調(diào)用的優(yōu)先級(jí):

我們這樣假設(shè)下,super(超類)、this(當(dāng)前類對(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è)子類ChildA ,繼承ParentCls :

class ChildA extends ParentCls{public String show(ChildA obj) {return 'ChildA';};public String show(ParentCls obj) {return 'ChildA and Parent';};}

寫(xiě)一個(gè)子類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 ChildAChildAChildAChildA

第一個(gè)輸出,p1是ParentCls的實(shí)例,且類ParentCls中有show(ChildA obj)方法,直接執(zhí)行該方法, ①有效;

第二個(gè)輸出,b是ChildB 的實(shí)例,類ChildB 中沒(méi)有show(ChildA obj)方法,①無(wú)效,再?gòu)腃hildB 的父類ChildA查找,ChildA中剛好有show(ChildA obj)方法,②有效;

第三個(gè)輸出,a是ChildA的實(shí)例,b是ChildB的實(shí)例,類ChildA中沒(méi)有show(ChildB obj)方法,①無(wú)效,再?gòu)腃hildA的父類ParentCls入手,ParentCls中也沒(méi)有show(ChildB obj)方法,②無(wú)效,從ChildB的父類入手,(super)ChildB 即是ChildA,所以a.show(b)=>a.show(a),ChildA中剛好有show(ChildA obj)方法,③有效;

④就不作演示,根據(jù)①②③很容易得出結(jié)論;

第四個(gè)輸出,體現(xiàn)Java多態(tài)性,符合①,但是p2是引用類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 staticthis is SonDemo staticthis is ParentDemo code blockthis is ParentDemo constructorthis is SonDemo code blockthis is SonDemo constructor

由上可見(jiàn),Java中 靜態(tài)塊中的代碼在類加載時(shí)執(zhí)行,子類繼承父類。會(huì)按照繼承的順序先執(zhí)行靜態(tài)代碼塊。當(dāng)實(shí)例化對(duì)象的時(shí)候,同理會(huì)按照繼承的順序依次執(zhí)行如下代碼:

代碼塊,構(gòu)造函數(shù),當(dāng)父類的執(zhí)行完以后,再執(zhí)行子類。

以上這篇淺談Java方法調(diào)用的優(yōu)先級(jí)問(wèn)題就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 亚洲一区二区免费视频 | 国产第6页| 日韩精品视频在线看 | 河智苑色即是空 | 午夜精品在线 | 国产精品97 | 亚洲狼人天堂 | 久久99久 | 中文字幕在线观看一区二区三区 | 国产青青操| 男人的天堂视频在线观看 | 激情小说qvod | 国产精品色婷婷99久久精品 | 国产成人福利在线 | 国产在线观看一区二区三区 | mm131午夜 | 97爱爱爱 | 美女网站在线看 | 国产成人自拍视频在线观看 | 中文字幕永久在线观看 | 美国黄色小视频 | 日韩成人激情 | 在线三级av| 日本aⅴ在线观看 | 男人激情网 | 97久久精品视频 | 精品欧美日韩 | 福利社午夜影院 | av先锋资源 | 一区二区高清在线 | 特级西西人体444是什么意思 | 在线观看色网站 | 欧美特级黄 | 国产在线啪 | 亚洲综合在线视频 | 国产精品久免费的黄网站 | 天堂在线免费观看视频 | 国产一区二区视频在线免费观看 | a国产视频 | 中文字幕av一区二区三区谷原希美 | 成人一区二区三区在线观看 |