尝试使用泛型类型创建静态字段无法编译:
class MyClass {
public static Function<Z, Z> blargh = new Function<Z, Z>() {
public Z apply(Z a) {
return a;
}
};
}
日食 说:
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
s 就可以了:
static Function<Integer, Integer> blargh = new Function<Integer, Integer>() {
public Integer apply(Integer a) {
return a;
}
};
这里发生了什么?
语境:
我最初是想弄清楚为什么这段代码使用方法而不是字段:
public static <T extends Throwable> F<T, String> eMessage() { return new F<T, String>() { public String f(final Throwable t) { return t.getMessage(); } }; }
也许是为了克服这个限制?
该
Function
类型来自谷歌的番石榴库。