Computer >> 컴퓨터 >  >> 프로그램 작성 >> Java

Java 9의 인터페이스에서 개인 메서드를 사용할 수 있습니까?

<시간/>

예, Java9부터 인터페이스에서 private 메소드를 사용할 수 있습니다.

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