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中文網其他相關文章!