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