有这样的设计:
interface Foo<T> {
void doSomething(T t);
}
class FooImpl implements Foo<Integer> {
//code...
}
interface Bar extends Foo {
//code...
}
class BarImpl extends FooImpl implements Bar {
//code...
}
它给了我编译错误:
接口 Foo 不能
多次实施
不同的参数: Foo 和
富
此问题的一个简单解决方法是:
interface Bar extends Foo<Integer> {
// code...
}
Bar 接口中的整数类型完全没有用。
有没有更好的方法来解决这个问题?
还有更好的设计吗?
感谢您的建议。
EDIT:
给出的解决方案:
> interface Bar<T> extends Foo<T>
没问题,但是和我之前的解决方案一样。我不需要在 Bar 中输入 T 。
让我举一个更好的例子:
interface ReadOnlyEntity {
}
interface ReadWriteEntity extends ReadOnlyEntity {
}
interface ReadOnlyDAO<T extends ReadOnlyEntity> {
}
interface ReadWriteDAO<K extends ReadWriteEntity, T extends ReadonlyEntity> extends ReadOnlyDAO<T> {
}
这是一个好的设计吗?
我建议考虑 Bar 泛型的类型或重新考虑您的设计。如果没有对 Bar 有意义的对象,那么它不应该被实现Foo<T>
.
EDIT:
这是一个好的设计吗?
不,我认为不是。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)