何為 Java 中的繫結?
方法呼叫的關聯及其方法體在 Java 中稱為繫結。繫結有兩類。
靜態繫結
靜態繫結中,方法呼叫在編譯時與方法體繫結。這也稱為早期繫結。這是使用靜態、私有和最終方法完成的。
示例
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()
}
}
輸出
This is the method of sub class
動態繫結
動態繫結中,方法呼叫在執行時與方法體繫結。這也稱為後期繫結。這是使用例項方法完成的。
示例
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()
}
}
輸出
This is the method of sub class
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP