interface I1 { ... }
interface I2 { ... }
interface I3 { ... }
interface I4 { ... }
interface MyFactory {
Object<? extends I1 & I2 & I3> createI1I2I3(); // doesn't work
Object<? extends I2 & I3 & I4> createI2I3I4(); // doesn't work
}
Is there a trick to do it? I was thinking about things like
interface I1I2I3 extends I1, I2, I3 { ... }
But I1I2I3
!= <? extends I1 & I2 & I3>
<? extends I1 & I2 & I3>
. There's a reason I just can't use this approach - I1
, I2
and I3
are foreign code.
Update
For those who curious why might someone need such a weird thing:
interface Clickable {}
interface Moveable {}
interface ThatHasText {}
interface Factory {
Object<? extends Clickable> createButton(); // just a button with no text on it
Object<? extends Clickable & ThatHasText> createButtonWithText();
Object<? extends Moveable & ThatHasText> createAnnoyingBanner();
}
Object
doesn't accept type parameter You can use the following construct instead:
interface I1 { }
interface I2 { }
interface I3 { }
interface I4 { }
interface MyFactory {
public <T extends I1 & I2 & I3> T createI1I2I3();
public <T extends I2 & I3 & I4> T createI2I3I4();
}
Your return type should be parameterized, so you can do
interface MyFactory {
<T extends I1 & I2 & I3> T createI1I2I3();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.