在Java中,有没有办法让一个泛型类型的接口扩展另一个泛型类型的接口?
我想创建一个这样的界面在Java中,有没有办法让一个泛型类型的接口扩展另一个泛型类型的接口?,java,Java,我想创建一个这样的界面 public interface MyInterface<T extends OtherInterface<K>>{ K doSomething(T o); } 公共接口MyInterface{ K剂量测定法(to); } 但是编译器不会识别它。 另一种方法是: public interface MyInterface<T extends OtherInterface<K>,K>{ K doSomethi
public interface MyInterface<T extends OtherInterface<K>>{
K doSomething(T o);
}
公共接口MyInterface{
K剂量测定法(to);
}
但是编译器不会识别它。
另一种方法是:
public interface MyInterface<T extends OtherInterface<K>,K>{
K doSomething(T o);
}
公共接口MyInterface{
K剂量测定法(to);
}
我的问题是,尽管第二个代码可以工作,但是否有一种方法与第一个代码类似,这样我就不必使用两种类型来宣布接口?如果您有一个带有两个类型参数的泛型接口,那么您需要在类签名中声明它们 或者,如果将
K
声明为通配符?
并仅返回T
,您仍然可以将输出T
强制转换到正确的接口。例如:
interface Foo<T> { }
interface Bar<T extends Foo<?>>{
T doSomething(T o);
}
class IntegerFoo implements Foo<Integer> {}
...
public static void main(String[] args) {
IntegerFoo integerFoo = new IntegerFoo();
Bar<IntegerFoo> bar = t -> t;
Foo<Integer> result = bar.doSomething(integerFoo);
}
接口Foo{}
接口巴诺。必须声明类型。而且T
很奇怪。通常情况下,您希望能够比较Ts:T extenss compariable
@markspace抱歉,这会造成混淆。Comparable只是这里的一个例子,它可以是列表或其他界面。@LuffyTse可能编辑问题并将Comparable
更改为MyOtherInterface
或您选择的内容-只是删除与Comparable