day09-接口的私有方法的定义和使用

This commit is contained in:
2026-01-23 14:42:46 +08:00
parent ff566ba105
commit 70bf078530
3 changed files with 62 additions and 0 deletions

View File

@@ -0,0 +1,9 @@
package com.inmind.interface_private04;
public class Demo04 {
public static void main(String[] args) {
MyInterfaceImpl myInterface = new MyInterfaceImpl();
myInterface.method1();
MyInterfaceP.methodS1();
}
}

View File

@@ -0,0 +1,4 @@
package com.inmind.interface_private04;
public class MyInterfaceImpl implements MyInterfaceP{
}

View File

@@ -0,0 +1,49 @@
package com.inmind.interface_private04;
/*
JDK9中提供了私有方法的使用
接口的私有方法定义和使用
格式:
private 返回值 私有方法名(参数列表){
}
作用:接口中私有方法的作用,是给接口中默认方法来调用的,也就是说私有方法是将默认方法中
相同的内容抽取出来,同时静态方法也可以做相同的操作
*/
public interface MyInterfaceP {
//默认方法
default void method1(){
commonMethod();
}
default void method2(){
commonMethod();
}
default void method3(){
commonMethod();
}
//私有方法来抽取相同的功能判断,给默认方法使用
private void commonMethod(){
System.out.println("hello");
System.out.println("world");
System.out.println("java");
}
//静态方法
static void methodS1(){
commonMethodS();
}
static void methodS2(){
commonMethodS();
}
static void methodS3(){
commonMethodS();
}
//私有方法来抽取相同的功能判断,给默认方法使用
private static void commonMethodS(){
System.out.println("hello");
System.out.println("world");
System.out.println("java");
}
}