2
私はエンティティーのクラス階層を持っており、Java用のサービスインターフェースの階層を作成したいと考えています。 UIコンポーネントは、次いで、インターフェースを介してエンティティに関連するサービスにアクセスしなければならない:(わずかに異なるコンテキスト内の複数の場所で再利用される)Java汎用インターフェース階層
class BaseEntity { }
class Fruit extends BaseEntity { }
class Banana extends Fruit { }
class Apple extends Fruit { }
UIコンポーネントは、インターフェースFruitService介しフルーツサービスにアクセスする必要があると私が決定します実行時には、これはBananaServiceまたはAppleServiceサービスインターフェイスになります。私は、これはジェネリックを使用して、簡単なことだろうと思った:
interface Service<T extends BaseEntity>
{
List<T> getAll();
void save (T object);
void delete (T object);
}
// More strict interface only allowed for fruits. Referenced by UI component
interface FruitService<F extends Fruit> extends Service<Fruit> {}
// Interface only allowed for bananas
interface BananaService extends FruitService<Banana> {}
class BananaServiceImpl implements BananaService
{
// Compiler error here because expecting Fruit type:
@Override
public List<Banana> getAll()
{
}
...
}
これは、しかし、私に次のコンパイラエラーを与えている:
The return type is incompatible with Service<Fruit>.getAll()
はなぜJavaは実装がバナナでパラメータ化されたことを認識しないのですか?私はBananaServiceで指定されたBananaServiceImplの汎用パラメータがBananaに解決されることを期待しています!
にかけジェネリック渡し、これはコンパイルエラーを修正。ありがとう! – Wombat