首頁  >  文章  >  Java  >  java靜態和動態綁定怎麼實現

java靜態和動態綁定怎麼實現

WBOY
WBOY轉載
2023-06-03 18:22:03975瀏覽

不同綁定的比較

1、靜態綁定發生在編譯期,動態綁定發生在運行期。

2、動態綁定的彈性高於靜態綁定,因為靜態綁定是在編譯過程中決定的,動態綁定在編譯過程中不知道要呼叫哪一種方法。

3、靜態綁定調用方法比動態綁定快,因為靜態綁定可以直接調用,動態綁定需要搜尋方法表。

實例

靜態綁定

class Super{
public static void sample(){
System.out.println("This is the method of super class");
 
}
 
}
 
Public class Sub extends Super{
Public static void sample(){
System.out.println("This is the method of sub class");
 
}
 
Public static void main(String args[]){
Sub.sample()
 
}
 
}

(2)動態綁定

class Super{
public void sample(){
System.out.println("This is the method of super class");
 
}
 
}
 
Public class extends Super{
Public static void sample(){
System.out.println("This is the method of sub class");
 
}
 
Public static void main(String args[]){
new Sub().sample()
 
}
 
}

以上是java靜態和動態綁定怎麼實現的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除