我需要一种好方法来维护用于菜单选择的多个表单级别数据。因此,例如,如果我有A和B,每个人都可能有1 2 3

这可以持续很长时间,这样我就可以有 A -> A1 -> A1.1 -> A1.1.1 -....我有以下课程,工作正常,但我怀疑我们可以有更好的。

我只需要在像 Widget 这样的选择树中执行选择,但是每个级别的选择都以另一种形式出现(在 J2ME 中)

import java.util.Vector;
 public class Tag {
    private String tag;
    private Vector childTags;
    private Tag parent;

    Tag(String tag, Vector childtag)
    {
        this.tag = tag;
        this.childTags= childTags;
    }

    public void setChildTags(Vector childTags) {
        this.childTags = childTags;
    }

    public Vector getChildTags() {
        return this.childTags;
    }

    public String getTag() {
        return this.tag;
    }


    public String toString(int depth)
    {
                String a  ="";
        if(depth==0)
        {
            a = a + this.getTag();
        }

        if(this.getChildTags()!= null)
        {

                    for(int k=0;k <this.getChildTags().capacity(); k++)
                      {
                                for (int i=0; i<depth; i++ ) {
                                        a = a + ("-");
                                }
                                a = a+ ( ((Tag)this.getChildTags().elementAt(k)).toString(depth++));
        }   }
    return a;
        }


}

没有正确的解决方案

其他提示

您可以使用一些类来表示您的菜单命令。将 1 2 3 和 A B C 视为命令以及组合它们的方式,因此,在这里应用一些模式,您可以使用 Command ( http://en.wikipedia.org/wiki/Command_pattern ) 和复合 ( http://en.wikipedia.org/wiki/Composite_pattern )。让它们一起工作,使用和维护它会更加简单,而不是神秘且容易出错的“childTag向量”。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top