
Java 9 在接口中添加了私有方法新功能。可以使用private修饰符来定义私有方法。我们可以在Java 9的接口中添加私有和私有静态方法
接口中私有方法的规则:
<strong>interface <interface-name> {
private methodName(parameters) {
// some statements
}
}</strong>interface TestInterface {
<strong>default </strong>void methodOne() {
System.out.println("This is a Default method One...");
printValues(); // calling a private method
}
<strong>default </strong>void methodTwo() {
System.out.println("This is a Default method Two...");
printValues(); // calling private method...
}
<strong>private </strong>void <strong>printValues</strong><strong>()</strong> { <strong>// private method in an interface
</strong> 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();
}
}<strong>This is a Default method One... methodOne() called methodTwo() called This is a Default method Two... methodOne() called methodTwo() called</strong>
以上就是在Java 9中,接口中的私有方法有哪些规则?的详细内容,更多请关注php中文网其它相关文章!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号