您不能给匿名类命名,这就是为什么它被称为“匿名”的原因。我看到的唯一选择是
final从您的外部范围引用变量
Callable
// Your outer loopfor (;;) { // Create some final declaration of `e` final E e = ... Callable<E> c = new Callable<E> { // You can have class variables private String x; // This is the only way to implement constructor logic in anonymous classes: {// do something with e in the constructor x = e.toString(); } E call(){ if(e != null) return e; else { // long task here.... } } }}另一个选择是像这样定义一个本地类(不是匿名类):
public void myMethod() { // ... class MyCallable<E> implements Callable<E> { public MyCallable(E e) { // Constructor } E call() { // Implementation... } } // Now you can use that "local" class (not anonymous) MyCallable<String> my = new MyCallable<String>("abc"); // ...}如果您还需要更多,请创建一个常规
MyCallable类…



