有类似这样的在这里回答: 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