天天看点

java 泛型的接口_Java泛型和接口

有这个设计:

interface Foo {

void doSomething(T t);

}

class FooImpl implements Foo {

//code...

}

interface Bar extends Foo {

//code...

}

class BarImpl extends FooImpl implements Bar {

//code...

}

它给了我编译错误:

The interface Foo cannot be

implemented more than once with

different arguments: Foo and

Foo

解决此问题的简单方法是:

interface Bar extends Foo {

// code...

}

Bar界面中的整数类型完全没用.

有没有更好的方法来解决这个问题?

任何更好的设计?

谢谢你的建议.

编辑:

给定解决方案

> interface Bar extends Foo

没关系,但和我之前的解决方案一样.我在Bar中不需要T型.

让我给出一个更好的样本:

interface ReadOnlyEntity {

}

interface ReadWriteEntity extends ReadOnlyEntity {

}

interface ReadOnlyDAO {

}

interface ReadWriteDAO extends ReadOnlyDAO {

}

这是一个很好的设计吗?