ここにこれに似た答えがありました: How to pass a function as a parameter in Java?
しかし、提供された正解は機能しません。私はクラスを持っています:
public class randomClass {
2
3 public String name;
4 private int x;
5 private boolean y;
6
7 public randomClass(String name){
8 this.name = name;
9 setAttributes(1,true, "test");
10 System.out.println(x + "," + y);
11 }
...
21
22 public int xMethod(){
23 return 1;
24 }
25
26 public void passMethod(){
27 testMethod(new Callable<Integer>() {
28 public Integer call(){
29 return xMethod();
30 }
31 });
32 }
33
34 public void testMethod(Callable<Integer> myFunc){
35
36 }
関数内passMethod
で に渡そうとしてxMethod
いtestMethod
ますが、エラーは次のとおりです。
シンボル symbol が見つかりません
: クラス Callable
理由はわかりません。
また、xMethod の戻り値の型を String にしてみましたが、戻り値の型が Integer と異なる関数を渡すことはできますか?