Java介面中允許的方法修飾符有哪些?


Java中的介面是方法原型的規範。每當需要指導程式設計師或制定一個契約,指定型別的欄位和方法應該如何使用時,都可以定義一個介面。

在Java 7中

在**Java 7**中,介面的方法只能使用`public`和`abstract`作為修飾符。

interface MyInterface{
   public abstract void display();
   public abstract void setName(String name);
   public abstract void setAge(int age);
}

在介面的方法中使用任何其他修飾符都會導致編譯時錯誤。

從Java 8開始

從**Java 8**開始,介面允許使用預設方法和靜態方法。

  • **靜態方法** - 靜態方法使用`static`關鍵字宣告,它將與類一起載入到記憶體中。可以使用類名訪問靜態方法,無需例項化。
  • 需要使用介面名稱呼叫介面的靜態方法。

示例

 線上演示

interface MyInterface{
   public void demo();
   public static void display() {
      System.out.println("This is a static method");
   }
}
public class InterfaceExample{
   public void demo() {
      System.out.println("This is the implementation of the demo method");
   }
   public static void main(String args[]) {
      InterfaceExample obj = new InterfaceExample();
      obj.demo();
      MyInterface.display();
   }
}

輸出

This is the implementation of the demo method
This is a static method
  • **預設方法** - 預設方法是介面方法的預設實現,如果介面中有預設方法,則無需在已實現此介面的類中實現它。
  • 預設方法也稱為防禦者方法**或**虛擬擴充套件方法。可以使用`default`關鍵字定義預設方法,例如:
default void display() {
   System.out.println("This is a default method");
}

示例

 線上演示

interface sampleInterface{
   public void demo();
   default void display() {
      System.out.println("This is a default method");
   }
}
public class DefaultMethodExample implements sampleInterface{
   public void demo() {
      System.out.println("This is the implementation of the demo method");
   }
   public static void main(String args[]) {
      DefaultMethodExample obj = new DefaultMethodExample();
      obj.demo();
      obj.display();
   }
}

輸出

This is the implementation of the demo method
This is a default method

從Java 9開始

從Java 9開始,介面允許使用私有方法和私有靜態方法。

示例

interface MyInterface {
   public abstract void demo();
   public default void defaultMethod() {
      privateMethod();
      staticPrivateMethod();
      System.out.println("This is a default method of the interface");
   }

   public static void staticMethod() {
      staticPrivateMethod();
      System.out.println("This is a static method of the interface");
   }

   private void privateMethod(){
      System.out.println("This is a private method of the interface");
   }

   private static void staticPrivateMethod(){
      System.out.println("This is a static private method of the interface");
   }
}

public class InterfaceMethodsExample implements MyInterface {
   public void demo() {
      System.out.println("Implementation of the demo method");
   }

   public static void main(String[] args){
      InterfaceMethodsExample obj = new InterfaceMethodsExample();
      obj.defaultMethod();
      obj.demo();
      MyInterface.staticMethod();
   }
}

輸出

This is a private method of the interface
This is a static private method of the interface
This is a default method of the interface
Implementation of the demo method
This is a static private method of the interface
This is a static method of the interface

更新於:2019年7月30日

3K+ 次瀏覽

啟動您的職業生涯

透過完成課程獲得認證

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