> Java > java지도 시간 > 본문

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

WBOY
풀어 주다: 2023-08-25 12:33:06
앞으로
866명이 탐색했습니다.

在Java 9中,我们可以在接口中使用私有方法吗?

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

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
로그인 후 복사

위 내용은 Java 9에서는 인터페이스에서 개인 메소드를 사용할 수 있습니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

원천:tutorialspoint.com
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!