day10--接口的私有方法的定义和使用
This commit is contained in:
@@ -0,0 +1,4 @@
|
|||||||
|
package com.inmind.interface_private04;
|
||||||
|
|
||||||
|
public class Demo04 {
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
package com.inmind.interface_private04;
|
||||||
|
|
||||||
|
/*
|
||||||
|
既有默认方法,和静态方法,有可能出现在一个接口中有很多这些方法
|
||||||
|
|
||||||
|
接口的私有方法的作用:对接口中默认方法来调用的,可以将相同的重复的功能代码进行抽取优化,也可以对静态方法进行抽取
|
||||||
|
|
||||||
|
私有方法的定义格式:
|
||||||
|
private (static) 返回值类型 方法名(参数列表) {
|
||||||
|
java方法体
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
public interface MyInterface {
|
||||||
|
public default void defaultMethod1(){
|
||||||
|
sameMethod();
|
||||||
|
System.out.println("defaultMethod1");
|
||||||
|
}
|
||||||
|
|
||||||
|
public default void defaultMethod2(){
|
||||||
|
sameMethod();
|
||||||
|
System.out.println("defaultMethod2");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void sameMethod(){
|
||||||
|
System.out.println("代码1");
|
||||||
|
System.out.println("代码2");
|
||||||
|
System.out.println("代码3");
|
||||||
|
System.out.println("代码4");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void staticMethod1(){
|
||||||
|
sameStaticMethod();
|
||||||
|
System.out.println("defaultMethod1");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void staticMethod2(){
|
||||||
|
sameStaticMethod();
|
||||||
|
System.out.println("defaultMethod2");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void sameStaticMethod(){
|
||||||
|
System.out.println("代码1");
|
||||||
|
System.out.println("代码2");
|
||||||
|
System.out.println("代码3");
|
||||||
|
System.out.println("代码4");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user