以下是一個範例,展示如何在Java 9介面中使用私有方法−
interface my_int{ public abstract void multiply_vals(int a, int b); public default void add_vals(int a, int b){ sub_vals(a, b); System.out.print("Default method result "); System.out.println(a + b); } private void sub_vals(int a, int b){ System.out.print("Private method result "); System.out.println(a - b); } private static void div(int a, int b){ System.out.print(" Private static method result "); System.out.println(a / b); } } public class my_new_int implements my_int{ @Override public void multiply_vals(int a, int b){ System.out.print("Abstract method result "); System.out.println(a * b); } public static void main(String[] args){ my_int in = new my_new_int(); in.multiply_vals(11, 34); in.add_vals(78, 0); } }
Abstract method result 374 Private method result 78 Default method result 78
An interface named 'my_int' is defined, that has an abstract function, without a body. Another 預設函數被定義為基本上將兩個數字相加。另一個名為‘sub_vals’的函數是 defined, which subtracts the two numbers.
Another static function named div is defined that divides the two values. A class named 'my_new_int' implements the previously defined interval. It overs the fface. In the main function, an instance of the interface is created, and the 'multiply_vals' function is called by passing specific values. Similarly, the 'add_vals' function is also) by passing specificon passing specificon 內容.
以上是Java 9介面中的私有方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!