[英]Type casting between two different classes
我有一个Propositions
类,它是类的数组列表
Proposition
:我想做一棵树,其节点来自类ConstituentSet
或Proposition
。 在Tree中,叶子仅来自Proposition
类,而所有内部Nodes来自ConstituentSet
类。
我不知道应该如何在类ConstituentSet
定义子类型。 如果我从类型定义ConstituentSet
,我不能把我的叶子在这种类型的(因为他们是从Proposition
),如果我从类型设置的儿童Proposition
,我不能把我的内部节点。
public class ConstituentSet<T> {
protected ConstituentSet<T> child1, child2;
//OR
protected Proposition child1,child2;
}
public class Proposition {
private Property property;
private Rating rating;
private Type type;
private Serializable value;
}
让您的Propositions
和ConstituentSet
实现一个公共接口 ,然后从该接口的实例构成一棵树。
实现一个接口
public interface TreeElement(){
// define methods
}
并通过此接口将Proposition
都实现为ConstituentSet
public class constituentSet implements TreeElement{
protected ArrayList<TreeElement> children;
// rest of code here
}
public class Proposition implements TreeElement{
// code here
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.