Java 类型参数后面不能跟其他边界解决方法

Java 类型参数后面不能跟其他边界解决方法,java,generics,Java,Generics,那么,有没有办法绕过这个限制,让DerivedA和DerivedB类的方法foo可以拥有我想要的签名 class SuperA{ } class SuperB{ } interface InterfaceA{ } interface InterfaceB<T>{ <P extends T & InterfaceA> void foo(P param); //error:type parameter cannot be followed by

那么,有没有办法绕过这个限制,让DerivedA和DerivedB类的方法
foo
可以拥有我想要的签名

class SuperA{
}

class SuperB{
}

interface InterfaceA{

}

interface InterfaceB<T>{
    <P extends T & InterfaceA> void foo(P param);
    //error:type parameter cannot be followed by other bounds
}

class DerivedA extends SuperA implements InterfaceB<SuperA>{

    @Override
    <P extends SuperA & InterfaceA> void foo(P param){
        //P should be some type extends SuperA and implements InterfaceA.
    }
}

class DerivedB extends SuperB implements InterfaceB<SuperB>{

    @Override
    <P extends SuperB & InterfaceA> void foo(P param){
        //P should be some type extends SuperB and implements InterfaceA.
    }
}
SuperA类{
}
一流的{
}
接口a{
}
接口B{
无效foo(P参数);
//错误:类型参数后面不能跟其他边界
}
类DerivedA扩展SuperA实现InterfaceB{
@凌驾
无效foo(P参数){
//P应该是扩展SuperA和实现InterfaceA的某种类型。
}
}
类DerivedB扩展了接口B{
@凌驾
void foo(P参数){
//P应该是某种类型,并实现InterfaceA。
}
}

据我所知,你想做的事是做不到的

您需要在创建时而不是以后限制
T

interface InterfaceB<T extends InterfaceA>{
    <P extends T> void foo(P param);
}
或者您可以创建
SuperA
SuperB
的子类来实现
InterfaceB


这是确保compiletime类型安全性的唯一方法。

有一些变通方法,但这些变通方法不能保证一切
保证(将来您可能会忘记并把事情搞得一团糟)。基本上,您只能在前一个类型上进行链接,并且只有第一个类型可以是任何组合,但是您只能在声明接口时在该类型上强制另一个接口

您可以在
InterfaceB

interface InterfaceB<T,T2 extends T>{ 
//T2 can only chain over T1
//but T1 can be anything, this case T1 extends Object, but it can extend any 1 Object + any multiple Interfaces 
    <T3 extends T2> void foo2(T3 param);
}

class DerivedA extends SuperA implements InterfaceB<SuperA,DerivedAWithInterfaceA>{ 
//this is kinda the only way to ensure that both SuperA+InterfaceA are used together 

    @Override
    public <T3 extends DerivedAWithInterfaceA> void foo2(T3 param) {}

}

class DerivedAWithInterfaceA extends DerivedA implements InterfaceA {
} 
//and now any SuperA+InterfaceA should extend this class instead of just SuperA
//idem class SuperB
接口b{
//T2只能在T1上连锁
//但T1可以是任何东西,本例中T1扩展了对象,但它可以扩展任意1个对象+任意多个接口
无效foo2(T3参数);
}
类DerivedA扩展SuperA实现InterfaceB{
//这是确保两个SuperA+InterfaceA一起使用的唯一方法
@凌驾
公共void foo2(T3参数){}
}
类DerivedAWithInterfaceA扩展DerivedA实现InterfaceA{
} 
//现在任何SuperA+InterfaceA都应该扩展这个类,而不仅仅是SuperA
//一流的
另一种方法是,但这一方法需要关注为每个子级声明的类型,但它使继承更加稳定:

class SuperA<T extends InterfaceA> implements InterfaceA{}

interface InterfaceA{}

interface InterfaceB<T,T2 extends T>{
    //T2 can only chain over T1
    //but T1 can be anything, this case T1 extends Object, but it can extend any 1 Object + any multiple Interfaces 
    <T3 extends T2> void foo2(T3 param);
    //T3 can chain or be like T and extend over something certain like T2 extends String&InterfaceA

    //void foo3(InterfaceB<? super InterfaceA,InterfaceA> example);
    //the only time you are allowed to use super is inside methods and only ? can use them(one the left, ie ? super T is allowed but T super ? not), but again this is only chaining
}

class DerivedA<T2 extends SuperA&InterfaceA> extends SuperA implements InterfaceB<SuperA,T2>{
//here you are making sure that T2 is always parent+InterfaceA and in children DerivedAChild extends DerivedA<T2 extends DerivedA&InterfaceA> is always this+InterfaceA
    @Override
    public <T3 extends T2> void foo2(T3 param) {
    }
}
类SuperA实现接口a{}
接口a{}
接口B{
//T2只能在T1上连锁
//但T1可以是任何东西,本例中T1扩展了对象,但它可以扩展任意1个对象+任意多个接口
无效foo2(T3参数);
//T3可以连锁或像T一样延伸到某些东西上,比如T2延伸字符串和接口A
//第3(b)条
class SuperA<T extends InterfaceA> implements InterfaceA{}

interface InterfaceA{}

interface InterfaceB<T,T2 extends T>{
    //T2 can only chain over T1
    //but T1 can be anything, this case T1 extends Object, but it can extend any 1 Object + any multiple Interfaces 
    <T3 extends T2> void foo2(T3 param);
    //T3 can chain or be like T and extend over something certain like T2 extends String&InterfaceA

    //void foo3(InterfaceB<? super InterfaceA,InterfaceA> example);
    //the only time you are allowed to use super is inside methods and only ? can use them(one the left, ie ? super T is allowed but T super ? not), but again this is only chaining
}

class DerivedA<T2 extends SuperA&InterfaceA> extends SuperA implements InterfaceB<SuperA,T2>{
//here you are making sure that T2 is always parent+InterfaceA and in children DerivedAChild extends DerivedA<T2 extends DerivedA&InterfaceA> is always this+InterfaceA
    @Override
    public <T3 extends T2> void foo2(T3 param) {
    }
}