首頁 > Java > java教程 > Java 9介面中的私有方法

Java 9介面中的私有方法

王林
發布: 2023-08-20 14:05:08
轉載
1287 人瀏覽過

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
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板