试图创建一个通用类型的静态字段不编译:为什么我不能声明一个参数化的静态类变量?
class MyClass {
public static Function<Z, Z> blargh = new Function<Z, Z>() {
public Z apply(Z a) {
return a;
}
};
}
Eclipse中说:
Multiple markers at this line
- Z cannot be resolved to a type
- Z cannot be resolved to a type
- Z cannot be resolved to a type
- Z cannot be resolved to a type
- The type new Function<Z,Z>(){} must implement the inherited
abstract method Function<Z,Z>.apply(Z)
但具体类型更换所有的Z
的作品就好了:
static Function<Integer, Integer> blargh = new Function<Integer, Integer>() {
public Integer apply(Integer a) {
return a;
}
};
这是怎么回事?
语境:
我原本是想弄清楚为什么this code使用方法,而不是一个领域:
public static <T extends Throwable> F<T, String> eMessage() { return new F<T, String>() { public String f(final Throwable t) { return t.getMessage(); } }; }
也许是克服这个限制?
Function
类型来自Google的番石榴图书馆。
@马特芬威克:你能告诉静态成员周围的代码?特别是你宣布的那个班级? – Tudor
是 - 已编辑OP。 –
@Matt Fenwick:谢谢,看看我的编辑是否更有意义。 – Tudor