
Java 9 在接口中添加了私有方法新功能。可以使用private修饰符来定义私有方法。我们可以在Java 9的接口中添加私有和私有静态方法
本文档主要讲述的是Android中JNI编程的那些事儿;JNI译为Java本地接口。它允许Java代码和其他语言编写的代码进行交互。在android中提供JNI的方式,让Java程序可以调用C语言程序。android中很多Java类都具有native接口,这些接口由本地实现,然后注册到系统中。希望本文档会给有需要的朋友带来帮助;感兴趣的朋友可以过来看看
接口中私有方法的规则:
- 私有方法的主体位于接口意味着我们不能像通常在接口中那样声明为普通的抽象方法。如果我们试图声明一个没有主体的私有方法,那么它可能会抛出一个错误,指出“此方法需要主体而不是分号”。
- 我们不能在接口中同时使用私有和抽象修饰符。
- 如果我们想从接口中的静态方法访问私有方法,那么方法可以声明为私有静态方法,因为我们无法对非静态方法进行静态引用。
- A 在非静态上下文中使用的私有静态方法意味着它可以从接口中的默认方法调用。
语法
interface{ private methodName(parameters) { // some statements } }
示例
interface TestInterface {
default void methodOne() {
System.out.println("This is a Default method One...");
printValues(); // calling a private method
}
default void methodTwo() {
System.out.println("This is a Default method Two...");
printValues(); // calling private method...
}
private void printValues() { // private method in an interface
System.out.println("methodOne() called");
System.out.println("methodTwo() called");
}
}
public class PrivateMethodInterfaceTest implements TestInterface {
public static void main(String[] args) {
TestInterface instance = new PrivateMethodInterfaceTest();
instance.methodOne();
instance.methodTwo();
}
}输出
This is a Default method One... methodOne() called methodTwo() called This is a Default method Two... methodOne() called methodTwo() called










