开发者

Extending both T and SomeInterface<T> in Java

开发者 https://www.devze.com 2022-12-25 05:33 出处:网络
I want to create a class that takes two parameters.One should be typed simply as T.The other should be typed as something that extends both T and SomeInterface<T>.When I attempt this with

I want to create a class that takes two parameters. One should be typed simply as T. The other should be typed as something that extends both T and SomeInterface<T>. When I attempt this with

public class SomeClass<T, S extends SomeInterface<T> & T>

then Java complains with

"The type T is not an inte开发者_如何学JAVArface; it cannot be specified as a bounded parameter"

and if instead I attempt to create an interface for S with

public interface TandSomeInterface<T> extends SomeInterface<T>, T

then Java complains with

"Cannot refer to the type parameter T as a supertype"

Is there any way to do this in Java? I think you can do it in C++...?


You can't create an interface that extends the type parameter T since there's no contract that would guarantee T to be an interface. And of course interface extending a class is not allowed.


this works if you extend an interface as well:

public class SomeClass<T extends I, S extends SomeInterface<T> & I>

but maybe it's not exactly what you want ...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号