有類似這樣的在這裏回答: 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
,但我得到的錯誤是:
cannot find symbol
symbol : class Callable
,我不知道爲什麼。
此外,我嘗試使用xMethod的返回類型字符串,你可以傳遞一個函數與不同的返回類型,然後整數?
你有沒有'進口java.util.concurrent.Callable'? (我假設這是你正在尋找的類) – amit
@amit:'java.util.concurrent.Callable'的接口在這種情況下可以,但它也會提示一個併發上下文,我不一定看這裏。像番石榴的功能界面會更普遍使用。 – nansen