创建引用父数组列表的方法

发布于 2024-11-19 05:58:02 字数 1810 浏览 3 评论 0原文

所以我问了一个与此类似的问题,但我认为我得到的答案与我想做的事情无关。

假设我有这个类:

Java 代码

public class Section
{
    private String sDocumentTitle;
    private String sHeadingTitle;
    private String sText;
    public ArrayList<Section> aSiblingSection = new ArrayList<Section>();
    public ArrayList<Section> aChildSection = new ArrayList<Section>();
    public ArrayList<image> aImages = new ArrayList<image>();

    public void setName(String docTitle)
    {
        //set passed parameter as name
        sDocumentTitle = docTitle;
    }

    public void addName (String docTitle)
    {
        //adds remaining Title String together
        sDocumentTitle += (" " + docTitle);
    }

    public String getName()
    {
        //return the set name
        return sDocumentTitle;
    }

    public void setSection(String section)
    {
        //set passed parameter as name
        sHeadingTitle = section;
    }

    public void addSection(String section)
    {
        //adds section parts together
        sHeadingTitle += ("" + section);
    }

    public String getSection()
    {
        //return the set name
        return sHeadingTitle;
    }
    public void setText(String text)
    {
        //set passed parameter as name
        sText = text;
    }

    public void addText(String text)
    {
        //adds 
        sText += (" " + text);
    }

    public String getText()
    {
        //return the set name
        return sText;
    }
    public ArrayList getChildSection()
    {
        return aChildSection;
    }
}  

以及在驱动程序类中以这种方式初始化的子部分...

Section aSection = new Section();
aMainSection.get(0).aChildSection.add(aSection);

本质上,有人可以告诉我如何在部分类中添加方法它从“aChildSection”的数组列表中返回父项?

So I had asked a question similar to this, but I don't think the answer I got worked with what I was trying to do.

Say I have this class:

Java Code

public class Section
{
    private String sDocumentTitle;
    private String sHeadingTitle;
    private String sText;
    public ArrayList<Section> aSiblingSection = new ArrayList<Section>();
    public ArrayList<Section> aChildSection = new ArrayList<Section>();
    public ArrayList<image> aImages = new ArrayList<image>();

    public void setName(String docTitle)
    {
        //set passed parameter as name
        sDocumentTitle = docTitle;
    }

    public void addName (String docTitle)
    {
        //adds remaining Title String together
        sDocumentTitle += (" " + docTitle);
    }

    public String getName()
    {
        //return the set name
        return sDocumentTitle;
    }

    public void setSection(String section)
    {
        //set passed parameter as name
        sHeadingTitle = section;
    }

    public void addSection(String section)
    {
        //adds section parts together
        sHeadingTitle += ("" + section);
    }

    public String getSection()
    {
        //return the set name
        return sHeadingTitle;
    }
    public void setText(String text)
    {
        //set passed parameter as name
        sText = text;
    }

    public void addText(String text)
    {
        //adds 
        sText += (" " + text);
    }

    public String getText()
    {
        //return the set name
        return sText;
    }
    public ArrayList getChildSection()
    {
        return aChildSection;
    }
}  

And a child section initialized in this manner in a driver class...

Section aSection = new Section();
aMainSection.get(0).aChildSection.add(aSection);

Essentially, could someone give me an idea of how I would I add a method in the section class which returns the parents from an array list of 'aChildSection'?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

美羊羊 2024-11-26 05:58:02

添加构造函数

private final Section parent;
public Section(Section parent) {
    this.parent = parent;
}

public Section getParent() {
    return parent;
}

当您添加子项时

Section aSection = new Section(aMainSection.get(0));
aMainSection.get(0).aChildSection.add(aSection);

Add a constructor

private final Section parent;
public Section(Section parent) {
    this.parent = parent;
}

public Section getParent() {
    return parent;
}

When you add a child

Section aSection = new Section(aMainSection.get(0));
aMainSection.get(0).aChildSection.add(aSection);
贵在坚持 2024-11-26 05:58:02

对于你的模型,你不能。添加父部分:

private Section parent;

并为每个子会话设置它(在父会话中它将为空)

With your model, you can't. Add a parent section:

private Section parent;

and set that for every child session (it will be null in the parent session)

我三岁 2024-11-26 05:58:02

我想,每个部分(主要部分除外)都有一个父级。诀窍是,一个部分需要知道它的父部分。

一种广泛使用的模式是使用构造函数设置父级,并向构造函数添加一些逻辑,以便它自动将该节注册为父级的子级:

public Section(Section parent) {
  this.parent = parent;   // remember your parent
  parent.addChild(this);  // register yourself as your parent's child
}

然后使用以下代码添加一个节:

Section mainSection = aMainSection.get(0);   // ugly!!
Section section = new Section(mainSection);

重构技巧 -将所有字段声明为私有并实现 getter。如果这些 getter 不返回内部列表而只返回列表中的值,那就更好了。

I guess, each section (except the main section) has one parent. The trick is, that a section needs to know it's parent section.

A widely used pattern is to set the parent with the constructor and add some logic to the constructor so that it will register the section as parent's child automatically:

public Section(Section parent) {
  this.parent = parent;   // remember your parent
  parent.addChild(this);  // register yourself as your parent's child
}

Then use this code to add a section:

Section mainSection = aMainSection.get(0);   // ugly!!
Section section = new Section(mainSection);

Refactoring tip - declare all your fields private and implement getters. Even better if those getters don't return the internal lists but just values from the list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文