ジェネリック型で静的フィールドを作成しようとしてもコンパイルされません:
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)
ただし、すべての s を具象型に置き換えるZ
と問題なく動作します。
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
タイプは Google の guava ライブラリからのものです。