Java 9 在介面中新增了私有##方法新功能。可以使用private修飾符來定義私有方法。我們可以在Java 9的介面中加入私有和私有#靜態方法
介面中私有方法的規則:
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
以上是在Java 9中,介面中的私有方法有哪些規則?的詳細內容。更多資訊請關注PHP中文網其他相關文章!