Java 未选中的重写返回类型
我有一个项目,它具有以下组件:
public abstract class BaseThing {
public abstract <T extends BaseThing> ThingDoer<T, String> getThingDoer();
}
public class SomeThing extends BaseThing {
public ThingDoer<SomeThing, String> getThingDoer() {
return Things.getSomeThingDoer();
}
}
public class SomeOtherThing extends BaseThing {
public ThingDoer<SomeOtherThing, String> getThingDoer() {
return Things.getSomeOtherThingDoer();
}
}
public class Things {
public ThingDoer<SomeThing, String> getSomeThingDoer {
return getThingDoer(SomeThing.class);
}
public ThingDoer<SomeOtherThing, String> getSomeOtherThingDoer {
return getThingDoer(SomeOtherThing.class);
}
private <D extends ThingDoer<T, String> D getThingDoer(Class<T> clazz) {
//get ThingDoer
}
}
public class ThingDoer<T, V> {
public void do(T thing) {
//do thing
}
}
public class DoThing {
private BaseThing thing;
public void doIt() {
thing.getThingDoer().do(thing);
}
}
我收到一个编译器警告,上面写着:SomeThing.getThingDoer()
未选中的覆盖:返回类型需要未经检查的转换。
已找到 ,必需
ThingDoer<SomeThing, String>
ThingDoer<T, String>
Everthing编译得很好,虽然我还没有机会进行测试,但我没有理由相信它不会起作用。DoThing.doIt()
我的问题是,这可以打破吗,有没有更好的方法来做到这一点?我可以创建一个基类,并为两者提供子类,但这似乎不是很优雅。DoThing
SomeThing
SomeOtherThing
编辑:我想避免使用通用。BaseThing