1. 程式人生 > >java的動態繫結與雙分派

java的動態繫結與雙分派

http://blog.csdn.net/zhengzhb/article/details/7496949

java的動態繫結

        所謂的動態繫結就是指程執行期間(而不是在編譯期間)判斷所引用物件的實際型別,根據其實際的型別呼叫其相應的方法。java繼承體系中的覆蓋就是動態繫結的,看一下如下的程式碼:

  1. class Father {  
  2.     publicvoid method(){  
  3.         System.out.println("This is Father's method");  
  4.     }  
  5. }  
  6. class Son1 extends Father{  
  7.     publicvoid method(){  
  8.         System.out.println("This is Son1's method");  
  9.     }  
  10. }  
  11. class Son2 extends Father{  
  12.     publicvoid method(){  
  13.         System.out.println("This is Son2's method");  
  14.     }  
  15. }  
  16. publicclass Test {  
  17.     publicstaticvoid main(String[] args){  
  18.         Father s1 = new
     Son1();  
  19.         s1.method();  
  20.         Father s2 = new Son2();  
  21.         s2.method();  
  22.     }  
  23. }  


執行結果如下:

This is Son1's method
This is Son2's method

       通過執行結果可以看到,儘管我們引用的型別是Father型別的,但是執行時卻是呼叫的它實際型別(也就是Son1和Son2)的方法,這就是動態繫結。在java語言中,繼承中的覆蓋就是是動態繫結的,當我們用父類引用例項化子類時,會根據引用的實際型別呼叫相應的方法。

java的靜態繫結

       相對於動態繫結,靜態繫結就是指在編譯期就已經確定執行哪一個方法。在java中,方法的過載(方法名相同而引數不同)就是靜態繫結的,過載時,執行哪一個方法在編譯期就已經確定下來了。看一下程式碼:

  1. class Father {}  
  2. class Son1 extends Father{}  
  3. class Son2 extends Father{}  
  4. class Execute {  
  5.     publicvoid method(Father father){  
  6.         System.out.println("This is Father's method");  
  7.     }  
  8.     publicvoid method(Son1 son){  
  9.         System.out.println("This is Son1's method");  
  10.     }  
  11.     publicvoid method(Son2 son){  
  12.         System.out.println("This is Son2's method");  
  13.     }  
  14. }  
  15. publicclass Test {  
  16.     publicstaticvoid main(String[] args){  
  17.         Father father = new Father();  
  18.         Father s1 = new Son1();  
  19.         Father s2 = new Son2();  
  20.         Execute exe = new Execute();  
  21.         exe.method(father);  
  22.         exe.method(s1);  
  23.         exe.method(s2);  
  24.     }  
  25. }  


執行結果如下:

This is Father's method
This is Father's method
This is Father's method

        在這裡,程式在編譯的時候就已經確定使用method(Father father)方法了,不管我們在執行的時候傳入的實際型別是什麼,它永遠都只會執行method(Father father)這個方法。也就是說,java的過載是靜態繫結的。

instanceof操作符與轉型

       有時候,我們希望在使用過載的時候,程式能夠根據傳入引數的實際型別動態地呼叫相應的方法,也就是說,我們希望java的過載是動態的,而不是靜態的。但是由於java的過載不是動態繫結,我們只能通過程式來人為的判斷,我們一般會使用instanceof操作符來進行型別的判斷。我們要對method(Father father)進行修改,在方法體中判斷執行期間的實際型別,修改後的method(Father father)方法如下:

  1. publicvoid method(Father father){  
  2.     if(father instanceof Son1){  
  3.         method((Son1)father);  
  4.     }elseif(father instanceof Son2){  
  5.         method((Son2)father);  
  6.     }elseif(father instanceof Father){  
  7.         System.out.println("This is Father's method");  
  8.     }  
  9. }  

        請注意,我們必須把判斷是否是父類的條件(也就是判斷是否為Father類的條件)放到最後,否則將一律會被判斷為Father類,達不到我們動態判斷的目的。修改程式碼後,程式就可以動態地根據引數的實際型別來呼叫相應的方法了。執行結果如下:

This is Father's method
This is Son1's method
This is Son2's method

        但是這種實現方式有一個明顯的缺點,它是偽動態的,仍然需要我們來通過程式來判斷型別。假如Father有100個子類的話,還是這樣來實現顯然是不合適的。必須通過其他更好的方式實現才行,我們可以使用雙分派方式來實現動態繫結。

用雙分派實現動態繫結

        類A中的方法method1和method2的區別就是,method2是雙分派。我們可以看一下java雙分派的特點:首先要有一個訪問類B,類B提供一個showA(A a) 方法,在方法中,呼叫類A的method1方法,然後類A的method2方法中呼叫類B的showA方法並將自己作為引數傳給showA。雙分派的核心就是這個this物件。說到這裡,我們已經明白雙分派是怎麼回事了,但是它有什麼效果呢?就是可以實現方法的動態繫結,我們可以對上面的程式進行修改,程式碼如下:

  1. class Father {  
  2.     publicvoid accept(Execute exe){  
  3.         exe.method(this);  
  4.     }  
  5. }  
  6. class Son1 extends Father{  
  7.     publicvoid accept(Execute exe){  
  8.         exe.method(this);  
  9.     }  
  10. }  
  11. class Son2 extends Father{  
  12.     publicvoid accept(Execute exe){  
  13.         exe.method(this);  
  14.     }  
  15. }  
  16. class Execute {  
  17.     publicvoid method(Father father){  
  18.         System.out.println("This is Father's method");  
  19.     }  
  20.     publicvoid method(Son1 son){  
  21.         System.out.println("This is Son1's method");  
  22.     }  
  23.     publicvoid method(Son2 son){  
  24.         System.out.println("This is Son2's method");  
  25.     }  
  26. }  
  27. publicclass Test {  
  28.     publicstaticvoid main(String[] args){  
  29.         Father father = new Father();  
  30.         Father s1 = new Son1();  
  31.         Father s2 = new Son2();  
  32.         Execute exe = new Execute();  
  33.         father.accept(exe);  
  34.         s1.accept(exe);  
  35.         s2.accept(exe);  
  36.     }  
  37. }  

        可以看到我們修改的地方,在Father,Son1,Son2中分別加入一個雙分派的方法。呼叫的時候,原本是呼叫Execute的method方法,現在改為呼叫Father的accept方法。執行結果如下:

This is Father's method
This is Son1's method
This is Son2's method

        執行結果符合我們的預期,實現了動態繫結。雙分派實現動態繫結的本質,就是在過載方法委派的前面加上了繼承體系中覆蓋的環節,由於覆蓋是動態的,所以過載就是動態的了,與使用instanceof操作符的效果是一樣的(用instanceof操作符可以實現過載方法動態繫結的原因也是因為instanceof操作符是動態的)。但是與使用instanceof操作符實現動態繫結相比,雙分派方式的可擴充套件性要好的多。