首頁  >  文章  >  Java  >  Java 9介面中的私有方法

Java 9介面中的私有方法

王林
王林轉載
2023-08-20 14:05:081242瀏覽

Java 9接口中的私有方法

以下是一個範例,展示如何在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);
   }
}

Output

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

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除