泛型树,自我有界泛型 [英] Generic tree, self bounded-generics
问题描述
我要为我的一个项目添加通用性。
我喜欢泛型,因为这使得我的代码更加健壮,自我记录并擦除所有那些丑陋的表演。
这基本上是某种通用树,具有双向链接(对于孩子和父母)。我最大限度地简化了类,以显示问题:
public class GenericTree<
ParentClass扩展GenericTree<?,?>,
ChildClass扩展GenericTree<?,?>>
{
//属性
private ArrayList< ChildClass> children = new ArrayList< ChildClass>();
private ParentClass parent = null;
//方法
public void setParent(ParentClass parent){
this.parent = parent;
}
public void addChild(ChildClass child){
child.setParent(this);
this.children.add(child);
$ / code>
问题在于指令:小孩.setParent(this)。
Java给出以下错误:
绑定不匹配:ChildClass类型的setParent(?)方法不适用于
参数(GenericTree)。通配符参数?没有低于
的界限,实际上可能比参数
更具限制性GenericTree
我想要的是能够表达如下:
public class GenericTree<
ParentClass扩展GenericTree<?,?>,
ChildClass扩展GenericTree< [THIS_VERY_CLASS],?>>
要说孩子类的父类应该是它自己的...
我看过一些关于自定义泛型的文章,但我不知道如何在这种情况下应用它。
任何帮助将不胜感激。
我能得到的最接近的是遵循Enum的自我引用模式。它仍然需要一个未经检查的强制转换,但是假设你的子类正确定义了T,它应该是安全的。
public class GenericTree< T extends GenericTree< T,P,C>,P扩展GenericTree< P,?,T>,C扩展GenericTree< C,T,>> {
//属性
private ArrayList< C> children = new ArrayList< C>();
private P parent = null;
//方法
public void setParent(P parent){
this.parent = parent;
}
$ b $ public void addChild(C child){
@SuppressWarnings(unchecked)
final T thisAsType =(T)this;
child.setParent(thisAsType);
this.children.add(child);
编辑:
执行示例 public static class SingleTypeTree< T>扩展
GenericTree< SingleTypeTree< T>,SingleTypeTree< T> ;, SingleTypeTree< T> {
}
I am to adding genericity to one of my projects.
I love generics as this makes my code more robust, self-documented and erases all those ugly casts.
However, I came across a tough case and have some issues trying to express a "recursive" constraint for one of my structures.
This is basically some kind of "generic" tree, with double links (to children and parent). I have simplified the class at maximum to show the issue :
public class GenericTree<
ParentClass extends GenericTree<?, ?>,
ChildClass extends GenericTree<?, ?>>
{
// Attributes
private ArrayList<ChildClass> children = new ArrayList<ChildClass>();
private ParentClass parent = null;
// Methods
public void setParent(ParentClass parent) {
this.parent = parent;
}
public void addChild(ChildClass child) {
child.setParent(this);
this.children.add(child);
}
}
The problem is for the instruction : child.setParent(this).
Java gives the following error :
Bound mismatch: The method setParent(?) of type ChildClass is not applicable for the
arguments (GenericTree). The wildcard parameter ? has no lower
bound, and may actually be more restrictive than argument
GenericTree
What I would like is to be able to express something like :
public class GenericTree<
ParentClass extends GenericTree<?, ?>,
ChildClass extends GenericTree<[THIS_VERY_CLASS], ?>>
To say that the parent class of the child class should be itself ...
I have looked at some articles about the self bounding generics, but I don't know how to apply it in this case.
Any help would be appreciated.
解决方案 The closest I could get is to follow Enum's pattern of self reference. It still requires an unchecked cast, but assuming your subclasses define T correctly, it should be safe.
public class GenericTree<T extends GenericTree<T, P, C>, P extends GenericTree<P, ?, T>, C extends GenericTree<C, T, ?>> {
// Attributes
private ArrayList<C> children = new ArrayList<C>();
private P parent = null;
// Methods
public void setParent(P parent) {
this.parent = parent;
}
public void addChild(C child) {
@SuppressWarnings("unchecked")
final T thisAsType = (T) this;
child.setParent(thisAsType);
this.children.add(child);
}
}
Edit:
Implementation example
public static class SingleTypeTree<T> extends
GenericTree<SingleTypeTree<T>, SingleTypeTree<T>, SingleTypeTree<T>> {
}
这篇关于泛型树,自我有界泛型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!