ジョン・C:
私はJavaで関数型プログラミングを試してみることにしようとしてきました。私はファーストクラス変数としてクラスで機能的なインターフェイスを使用する場合、私は、コンパイル時にしかし、私の変数が認識されません。
私はそれ内のローカル変数にすることを試みてきたmain
が、同じ結果を受け取りました。
私はここで何かが足りないのですか?
コード:
import java.util.function.BiFunction;
class Question {
static final BiFunction<Integer, Integer, Integer> add = (a,b) -> a+b;
public static void main(String[] args) {
System.out.println(Question.add(1,2));
}
}
エラー受付します:
Question.java:7: error: cannot find symbol
System.out.println(Question.add(1,2));
^
symbol: method add(int,int)
location: class Question
バージョン情報:
javac 11.0.6
openjdk 11.0.6 2020-01-14
Ubuntu 18.04
アンドリューTobilko:
Question.add(1,2)
一方、メソッド呼び出しは、あるadd
分野です。
class Question {
static final BiFunction<Integer, Integer, Integer> add = (a, b) -> a+b;
static final int add(int a, int b) {
return add.apply(a, b);
}
public static void main(String[] args) {
// calls the method
System.out.println(Question.add(1,2));
// gets the field and calls BiFunction's method
System.out.println(Question.add.apply(1,2));
}
}