Heim > Java > javaLernprogramm > Hauptteil

Können wir in Java 9 private Methoden in Schnittstellen verwenden?

WBOY
Freigeben: 2023-08-25 12:33:06
nach vorne
838 Leute haben es durchsucht

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

Ja, seit Java9 können Sie private Methoden in Schnittstellen verwenden.

Beispiel

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();
   }
}
Nach dem Login kopieren

Ausgabe

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
Nach dem Login kopieren

Das obige ist der detaillierte Inhalt vonKönnen wir in Java 9 private Methoden in Schnittstellen verwenden?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Quelle:tutorialspoint.com
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!