茄子在线看片免费人成视频,午夜福利精品a在线观看,国产高清自产拍在线观看,久久综合久久狠狠综合

    <s id="ddbnn"></s>
  • <sub id="ddbnn"><ol id="ddbnn"></ol></sub>

  • <legend id="ddbnn"></legend><s id="ddbnn"></s>

    訪問者模式討論篇:java的動態(tài)綁定與雙分派
    來源:易賢網(wǎng) 閱讀:1004 次 日期:2016-06-27 10:47:20
    溫馨提示:易賢網(wǎng)小編為您整理了“訪問者模式討論篇:java的動態(tài)綁定與雙分派”,方便廣大網(wǎng)友查閱!

    java的動態(tài)綁定

    所謂的動態(tài)綁定就是指程執(zhí)行期間(而不是在編譯期間)判斷所引用對象的實(shí)際類型,根據(jù)其實(shí)際的類型調(diào)用其相應(yīng)的方法。java繼承體系中的覆蓋就是動態(tài)綁定的,看一下如下的代碼:

    class father {

    public void method(){

    system.out.println(this is father's method);

    }

    }

    class son1 extends father{

    public void method(){

    system.out.println(this is son1's method);

    }

    }

    class son2 extends father{

    public void method(){

    system.out.println(this is son2's method);

    }

    }

    public class test {

    public static void main(string[] args){

    father s1 = new son1();

    s1.method();

    father s2 = new son2();

    s2.method();

    }

    }

    運(yùn)行結(jié)果如下:

    this is son1′s method

    this is son2′s method

    通過運(yùn)行結(jié)果可以看到,盡管我們引用的類型是father類型的,但是運(yùn)行時(shí)卻是調(diào)用的它實(shí)際類型(也就是son1和son2)的方法,這就是動態(tài)綁定。在java語言中,繼承中的覆蓋就是是動態(tài)綁定的,當(dāng)我們用父類引用實(shí)例化子類時(shí),會根據(jù)引用的實(shí)際類型調(diào)用相應(yīng)的方法。

    java的靜態(tài)綁定

    相對于動態(tài)綁定,靜態(tài)綁定就是指在編譯期就已經(jīng)確定執(zhí)行哪一個(gè)方法。在java中,方法的重載(方法名相同而參數(shù)不同)就是靜態(tài)綁定的,重載時(shí),執(zhí)行哪一個(gè)方法在編譯期就已經(jīng)確定下來了??匆幌麓a:

    class father {}

    class son1 extends father{}

    class son2 extends father{}

    class execute {

    public void method(father father){

    system.out.println(this is father's method);

    }

    public void method(son1 son){

    system.out.println(this is son1's method);

    }

    public void method(son2 son){

    system.out.println(this is son2's method);

    }

    }

    public class test {

    public static void main(string[] args){

    father father = new father();

    father s1 = new son1();

    father s2 = new son2();

    execute exe = new execute();

    exe.method(father);

    exe.method(s1);

    exe.method(s2);

    }

    }

    運(yùn)行結(jié)果如下:

    this is father’s method

    this is father’s method

    this is father’s method

    在這里,程序在編譯的時(shí)候就已經(jīng)確定使用method(father father)方法了,不管我們在運(yùn)行的時(shí)候傳入的實(shí)際類型是什么,它永遠(yuǎn)都只會執(zhí)行method(father father)這個(gè)方法。也就是說,java的重載是靜態(tài)綁定的。

    instanceof操作符與轉(zhuǎn)型

    有時(shí)候,我們希望在使用重載的時(shí)候,程序能夠根據(jù)傳入?yún)?shù)的實(shí)際類型動態(tài)地調(diào)用相應(yīng)的方法,也就是說,我們希望java的重載是動態(tài)的,而不是靜態(tài)的。但是由于java的重載不是動態(tài)綁定,我們只能通過程序來人為的判斷,我們一般會使用instanceof操作符來進(jìn)行類型的判斷。我們要對method(father father)進(jìn)行修改,在方法體中判斷運(yùn)行期間的實(shí)際類型,修改后的method(father father)方法如下:

    public void method(father father){

    if(father instanceof son1){

    method((son1)father);

    }else if(father instanceof son2){

    method((son2)father);

    }else if(father instanceof father){

    system.out.println(this is father's method);

    }

    }

    請注意,我們必須把判斷是否是父類的條件(也就是判斷是否為father類的條件)放到最后,否則將一律會被判斷為father類,達(dá)不到我們動態(tài)判斷的目的。修改代碼后,程序就可以動態(tài)地根據(jù)參數(shù)的實(shí)際類型來調(diào)用相應(yīng)的方法了。運(yùn)行結(jié)果如下:

    this is father’s method

    this is son1′s method

    this is son2′s method

    但是這種實(shí)現(xiàn)方式有一個(gè)明顯的缺點(diǎn),它是偽動態(tài)的,仍然需要我們來通過程序來判斷類型。假如father有100個(gè)子類的話,還是這樣來實(shí)現(xiàn)顯然是不合適的。必須通過其他更好的方式實(shí)現(xiàn)才行,我們可以使用雙分派方式來實(shí)現(xiàn)動態(tài)綁定。

    用雙分派實(shí)現(xiàn)動態(tài)綁定

    首先,什么是雙分派?還記得23種設(shè)計(jì)模式(9):訪問者模式中一開始舉的例子嗎?

    類a中的方法method1和method2的區(qū)別就是,method2是雙分派。我們可以看一下java雙分派的特點(diǎn):首先要有一個(gè)訪問類b,類b提供一個(gè)showa(a a) 方法,在方法中,調(diào)用類a的method1方法,然后類a的method2方法中調(diào)用類b的showa方法并將自己作為參數(shù)傳給showa。雙分派的核心就是這個(gè)this對象。說到這里,我們已經(jīng)明白雙分派是怎么回事了,但是它有什么效果呢?就是可以實(shí)現(xiàn)方法的動態(tài)綁定,我們可以對上面的程序進(jìn)行修改,代碼如下:

    class father {

    public void accept(execute exe){

    exe.method(this);

    }

    }

    class son1 extends father{

    public void accept(execute exe){

    exe.method(this);

    }

    }

    class son2 extends father{

    public void accept(execute exe){

    exe.method(this);

    }

    }

    class execute {

    public void method(father father){

    system.out.println(this is father's method);

    }

    public void method(son1 son){

    system.out.println(this is son1's method);

    }

    public void method(son2 son){

    system.out.println(this is son2's method);

    }

    }

    public class test {

    public static void main(string[] args){

    father father = new father();

    father s1 = new son1();

    father s2 = new son2();

    execute exe = new execute();

    father.accept(exe);

    s1.accept(exe);

    s2.accept(exe);

    }

    }

    可以看到我們修改的地方,在father,son1,son2中分別加入一個(gè)雙分派的方法。調(diào)用的時(shí)候,原本是調(diào)用execute的method方法,現(xiàn)在改為調(diào)用father的accept方法。運(yùn)行結(jié)果如下:

    this is father’s method

    this is son1′s method

    this is son2′s method

    運(yùn)行結(jié)果符合我們的預(yù)期,實(shí)現(xiàn)了動態(tài)綁定。雙分派實(shí)現(xiàn)動態(tài)綁定的本質(zhì),就是在重載方法委派的前面加上了繼承體系中覆蓋的環(huán)節(jié),由于覆蓋是動態(tài)的,所以重載就是動態(tài)的了,與使用instanceof操作符的效果是一樣的(用instanceof操作符可以實(shí)現(xiàn)重載方法動態(tài)綁定的原因也是因?yàn)閕nstanceof操作符是動態(tài)的)。但是與使用instanceof操作符實(shí)現(xiàn)動態(tài)綁定相比,雙分派方式的可擴(kuò)展性要好的多。

    更多信息請查看網(wǎng)頁制作
    易賢網(wǎng)手機(jī)網(wǎng)站地址:訪問者模式討論篇:java的動態(tài)綁定與雙分派
    由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

    2026國考·省考課程試聽報(bào)名

    • 報(bào)班類型
    • 姓名
    • 手機(jī)號
    • 驗(yàn)證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機(jī)站點(diǎn) | 投訴建議
    工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
    云南網(wǎng)警備案專用圖標(biāo)
    聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
    咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
    云南網(wǎng)警報(bào)警專用圖標(biāo)