我们可以在Java 9的接口中使用私有方法吗?
是的,自Java9开始,您可以在接口中使用私有方法。
示例
interface MyInterface {
public abstract void demo();
public default void defaultMethod() {
privateMethod();
staticPrivateMethod();
System.out.println("This is a default method of the interface");
}
public static void staticMethod() {
staticPrivateMethod();
System.out.println("This is a static method of the interface");
}
private void privateMethod(){
System.out.println("This is a private method of the interface");
}
private static void staticPrivateMethod(){
System.out.println("This is a static private method of the interface");
}
}
public class InterfaceMethodsExample implements MyInterface {
public void demo() {
System.out.println("Implementation of the demo method");
}
public static void main(String[] args){
InterfaceMethodsExample obj = new InterfaceMethodsExample();
obj.defaultMethod();
obj.demo();
MyInterface.staticMethod();
}
}输出结果
This is a private method of the interface This is a static private method of the interface This is a default method of the interface Implementation of the demo method This is a static private method of the interface This is a static method of the interface
热门推荐
3 入学校简短祝福语
10 致姐姐生日祝福语简短
11 老公生日祝福语简短好看
12 兔年祝福语简短100字
13 春节拜年祝福语儿童简短
14 幼儿节日祝福语大全简短
15 给舍友送祝福语简短
16 工人生日祝福语简短
17 祝福语美甲店员工简短
18 新婚抖音祝福语简短