diff --git a/day10/src/com/inmind/interface_private04/Demo04.java b/day10/src/com/inmind/interface_private04/Demo04.java new file mode 100644 index 0000000..cf8d18c --- /dev/null +++ b/day10/src/com/inmind/interface_private04/Demo04.java @@ -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(); + } +} diff --git a/day10/src/com/inmind/interface_private04/MyInterfaceImpl.java b/day10/src/com/inmind/interface_private04/MyInterfaceImpl.java new file mode 100644 index 0000000..522832c --- /dev/null +++ b/day10/src/com/inmind/interface_private04/MyInterfaceImpl.java @@ -0,0 +1,4 @@ +package com.inmind.interface_private04; + +public class MyInterfaceImpl implements MyInterfaceP{ +} diff --git a/day10/src/com/inmind/interface_private04/MyInterfaceP.java b/day10/src/com/inmind/interface_private04/MyInterfaceP.java new file mode 100644 index 0000000..ab222c3 --- /dev/null +++ b/day10/src/com/inmind/interface_private04/MyInterfaceP.java @@ -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"); + } +}