2012-09-30 155 views
0

有類似這樣的在這裏回答: 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我試圖通過xMethodtestMethod,但我得到的錯誤是:

cannot find symbol
symbol : class Callable

,我不知道爲什麼。

此外,我嘗試使用xMethod的返回類型字符串,你可以傳遞一個函數與不同的返回類型,然後整數?

+5

你有沒有'進口java.util.concurrent.Callable'? (我假設這是你正在尋找的類) – amit

+0

@amit:'java.util.concurrent.Callable'的接口在這種情況下可以,但它也會提示一個併發上下文,我不一定看這裏。像番石榴的功能界面會更普遍使用。 – nansen

回答

0

我認爲你需要導入Callable作爲import java.util.concurrent.Callable;,然後在你的課堂上使用它之後才能使用它。

入住這裏:

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/Callable.html

http://kamleshkr.wordpress.com/2009/10/02/java-threads-callable-and-future/

+0

是的,這使得錯誤消失,但在docs.oracle中的哪裏說輸入路徑? – Saad

+0

在指定名稱的頂部,您可以看到包含該名稱的包以及應在代碼中導入的包。 – heretolearn