day09-接口的私有方法的定义和使用
This commit is contained in:
9
day10/src/com/inmind/interface_private04/Demo04.java
Normal file
9
day10/src/com/inmind/interface_private04/Demo04.java
Normal 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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.inmind.interface_private04;
|
||||
|
||||
public class MyInterfaceImpl implements MyInterfaceP{
|
||||
}
|
||||
49
day10/src/com/inmind/interface_private04/MyInterfaceP.java
Normal file
49
day10/src/com/inmind/interface_private04/MyInterfaceP.java
Normal 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");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user