繁体   English   中英

在两个不同的类之间进行类型转换

[英]Type casting between two different classes

我有一个Propositions类,它是类的数组列表

Proposition :我想做一棵树,其节点来自类ConstituentSetProposition 在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;
}

让您的PropositionsConstituentSet实现一个公共接口 ,然后从该接口的实例构成一棵树。

实现一个接口

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM