如何使用枚举或其他方式在java中构建类别的层次结构树? [英] How to build an hierarchy tree of categories in java using enums or any other way?

查看:273
本文介绍了如何使用枚举或其他方式在java中构建类别的层次结构树?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一组类别:categories = {A,B}。我们假设A包含子类别:{A1,A2,A3}和B由子类别{B1,B2}组成。另外还有更多的子类别如下:对于A1:{A1a,A1b},对于A2 :对于B2,{A2a,A2b},对于A3:{A3a,A3b,A3c},B1:{B1a,B1b,B1c},B2:{B2a,B2b}。如何在java中构建层次结构?



由于每个集合的基数是预先已知的,我的初始方法是使用枚举类型,而不是使用继承来构建类,但是我可以接受任何建议。



提前感谢

解决方案

可能是这样的一个实现:

  public interface Category {
String getName();
类别getParent();
列表<类别> getSiblings();
列表<类别>的getChildren();
列表<类别> getDescendants();
void addChild(Category category);
void addChildren(List< Category> categories);
}


Assuming that we have a set of categories: categories={A,B}. Let's assume more that A consists of subcategories: {A1,A2,A3} and B consists of subcategories: {B1,B2}.In addition, there are even more subcategories as follows: for A1:{A1a,A1b}, for A2:{A2a,A2b}, for A3:{A3a,A3b,A3c}, for B1:{B1a,B1b,B1c}, for B2:{B2a,B2b}. How can I build an hierarchical structure in java?

Since the cardinality of each set is fixed and known in advance, my initial approach is to use enum types instead of building classes with inheritance, but I am open to any suggestion. I do not know how to approach this problem.

Thanks in advance.

解决方案

Probably an implementation of this:

public interface Category {
    String getName();
    Category getParent();
    List<Category> getSiblings();
    List<Category> getChildren();
    List<Category> getDescendants();
    void addChild(Category category);
    void addChildren(List<Category> categories);
}

这篇关于如何使用枚举或其他方式在java中构建类别的层次结构树?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆