Java 中的 IntUnaryOperator 介面


IntUnaryOperator 介面是 Java 的一個函式式介面,它對單個整數值運算元執行操作,並返回一個整數值作為結果。由於它是一個函式式介面,因此我們可以將其用作 lambda 表示式或方法引用的賦值目標。這裡的函式式介面是指只包含一個抽象方法並表現出單一功能的介面。一些函式式介面的例子包括 Predicate、Runnable 和 Comparable 介面。IntUnaryOperator 介面定義在 'java.util.function' 包中。在本文中,我們將藉助示例程式來探討 IntUnaryOperator 介面及其內建方法。

Java 中的 IntUnaryOperator 介面

在 Java 中,IntUnaryOperator 介面提供以下方法:

  • applyAsInt()

  • identity()

  • compose()

  • andThen()

我們將逐一討論這些方法,但在此之前,讓我們先看看 IntUnaryOperator 介面的語法。

語法

public interface IntUnaryOperator

在我們的程式中使用此介面之前,必須匯入它。要匯入 IntUnaryOperator 介面,請使用以下命令:

import java.util.function.IntUnaryOperator;

IntUnaryOperator 的 applyAsInt() 方法的使用

它是 IntUnaryOperator 介面中唯一的一個抽象方法,它接收一個整數作為引數,並在將運算子應用於給定運算元後返回一個整數作為結果。

語法

int applyAsInt(int argument);

示例

以下示例演示了 applyAsInt() 方法的實際實現。

import java.util.function.IntUnaryOperator;
public class Example1 {
   public static void main(String []args) {
      IntUnaryOperator op_1 = a -> 3 * a; // instance of IntUnaryOperator
      System.out.println("The applyAsInt function :");
      // using applyAsInt method  
      System.out.println(op_1.applyAsInt(56));
   }
}

輸出

The applyAsInt function :
168

IntUnaryOperator 的 identity() 方法的使用

它是 Java 中 IntUnaryOperator 介面的一個靜態方法,它返回一個始終返回其給定引數的單目運算子。

語法

IntUnaryOperator.identity();

示例

在下面的示例中,我們將展示 identity() 方法的使用。

import java.util.function.IntUnaryOperator;
public class Example2 {
   public static void main(String []args) {
      // creating instance of IntUnaryOperator using identity()
      IntUnaryOperator op_2 = IntUnaryOperator.identity();
      System.out.println("The identity function :");
      // calling applyAsInt method
      System.out.println(op_2.applyAsInt(56));
   }
}

輸出

The identity function :
56

IntUnaryOperator 的 compose() 方法的使用

它在計算傳遞給此方法的引數後始終返回一個組合的 IntUnaryOperator。接下來,將透過應用此組合的 IntUnaryOperator 來計算第一個 IntUnaryOperator。

語法

instance.compose(expression);

示例

以下示例顯示了 compose() 方法的使用。當我們傳遞運算元時,首先執行 compose() 方法,然後將結果應用於 IntUnaryOperator 的原始例項。

import java.util.function.IntUnaryOperator;
public class Example3 {
   public static void main(String []args) {
      // creating instance of IntUnaryOperator 
      IntUnaryOperator op_3 = a -> a / 6;
      System.out.println("The compose function :");
      // calling compose method
      op_3 = op_3.compose(a -> a * 9);
      // to print the result
      System.out.println(op_3.applyAsInt(56));
   }
}

輸出

The compose function :
84

IntUnaryOperator 的 andThen() 方法的使用

與 compose() 方法一樣,它也返回給定操作的組合 IntUnaryOperator。但是,當我們使用 andThen() 方法時,將首先計算原始 IntUnaryOperator,然後計算 andThen() 方法的表示式。

語法

instance.addThen(expression);

示例

在下面的示例中,我們將演示 andThen() 方法的使用。

import java.util.function.IntUnaryOperator;
public class Example4 {
   public static void main(String []args) {
      // creating instance of IntUnaryOperator 
      IntUnaryOperator op_4 = a -> 3 * a;
      System.out.println("The andThen function :");
      // calling addThen method
      op_4 = op_4.andThen(a -> 5 * a);
      // to print the result
      System.out.println(op_4.applyAsInt(56));
   }
}

輸出

The andThen function :
840

結論

在本文中,我們學習了 IntUnaryOperator 介面及其內建方法。它只有一個名為 'applyAsInt()' 的抽象方法,該方法接收一個整數引數並返回一個整數作為結果。另外兩個方法 'andThen()' 和 'compose()' 方法返回指定操作的組合 IntUnaryOperator。

更新於:2023年8月10日

瀏覽量:208

開啟您的職業生涯

完成課程獲得認證

開始學習
廣告