何為 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

更新時間: 30-7-2019

5K+ 瀏覽量

開啟 職業生涯

完成課程,獲得認證

開始學習
廣告
© . All rights reserved.