継承を使用していくつかのジェネリックを作成しましたが、タイプセーフに動作させることができません。私の目標は、オブジェクトが であるかであるかに関係なく、メソッドBaseService
を実行できるという一般的なことを提供することです。action(base)
base
Foo extends Base
Bar extends Base
以下は機能しません。なぜですか?
class Base;
class Foo extends Base;
interface BaseService<T extends Base> {
void action(T base);
}
class FooService implements BaseService<Foo> {
void action(Foo foo) {
}
}
//usage:
//complains that the method is not applicable for the argument,
//and that I should change action(T) to action(Base). Why?
Base base;
getService().action(base);
//just to demonstrate the problem
BaseService<? extends Base> getService() {
return new FooService();
}