[英]java, generics - how to parametrize a class with a type T which BOTH: extends and implements sth
I want to type class members, as well as args for methods, in a type T, which has to be a Node and implement my own interface Linkable. 我想在类型T中键入类成员以及方法的args,它必须是一个Node并实现我自己的接口Linkable。
Essentially, I would like to limit the vars to the small section of the inheritance 'tree' between Node and Linkable. 本质上,我想将变量限制在Node和Linkable之间的继承“树”的小部分。 Reason being I don't want to have to do all the casting inside the class, depending if I need to call methods of Node or methods of Linkable.
原因是我不想在类中进行所有的转换,这取决于我是否需要调用Node的方法或Linkable的方法。
At the moment in my project I know I'm using only Nodes which implement Linkable, but this condition is not enforced in any way. 在我的项目中,我知道我只使用实现Linkable的节点,但这种情况不会以任何方式强制执行。
Pseudo-code below (only for illustration): 下面的伪代码(仅用于说明):
public class MyClass <T extends ??> {
T node1;
Bounds bounds1;
bounds1 = node1.boundsInParentProperty(); //Node specific method
node1.linkableSpecificMethod(); //method defined in Linkable interface
//....
}
There are two things that help you: 有两件事可以帮助你:
That said, what you probably want is this: 那就是说,你可能想要的是:
class YourClass<T extends Node & Linkable> {
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.