編集の要約なし |
編集の要約なし |
||
14行目: | 14行目: | ||
} | } | ||
} | } | ||
===SAMインターフェース=== | |||
{| class="wikitable" summary="SAMインターフェース" | |||
|- | |||
! SAM !! 戻り値 !! 引数 !! 利用メソッド | |||
|- | |||
| [[IntBinaryOperator]] || int || int, int || applyAsInt | |||
|- | |||
| [[IntToLongFunction]] || long || int || applyAsLong | |||
|- | |||
| [[IntToDoubleFunction]] || double || int || applyAsDouble | |||
|- | |||
| [[IntConsumer]] || int || なし || getAsInt | |||
|- | |||
| [[IntPredicate]] || boolean || int || test | |||
|} |
2019年7月2日 (火) 15:31時点における版
< Java基本文法
- メソッドへの参照を変数に割り当てる事でそのメソッドを手軽に利用できる
- 機能オブジェクトを利用するには同じ引数と戻り値を定義したSAMインターフェースを使う
import java.util.function.*; public class Main { public static int sub(int a, int b) { return a - b; } public static void main(String[] args) { IntBinaryOperator func = Main::sub; int ans = func.applyAsInt(5, 3); System.out.println("5 - 3" + ans); } }
SAMインターフェース
SAM | 戻り値 | 引数 | 利用メソッド |
---|---|---|---|
IntBinaryOperator | int | int, int | applyAsInt |
IntToLongFunction | long | int | applyAsLong |
IntToDoubleFunction | double | int | applyAsDouble |
IntConsumer | int | なし | getAsInt |
IntPredicate | boolean | int | test |