編集の要約なし |
編集の要約なし |
||
14行目: | 14行目: | ||
System.out.println("5 - 3" + ans); | System.out.println("5 - 3" + ans); | ||
} | } | ||
} | |||
==例== | |||
public class Main { | |||
public static void main(String[] args) { | |||
Func1 f1 = FuncList::isOdd; | |||
Func2 f2 = FuncList::hoge; | |||
System.out.println(f1.call(15)); | |||
System.out.println(f2.call(true, "Smith")); | |||
} | } |
2019年7月2日 (火) 18:07時点における版
< 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); } }
例
public class Main { public static void main(String[] args) { Func1 f1 = FuncList::isOdd; Func2 f2 = FuncList::hoge; System.out.println(f1.call(15)); System.out.println(f2.call(true, "Smith")); }