创建引用父数组列表的方法
所以我问了一个与此类似的问题,但我认为我得到的答案与我想做的事情无关。
假设我有这个类:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
添加构造函数
当您添加子项时
Add a constructor
When you add a child
对于你的模型,你不能。添加父部分:
并为每个子会话设置它(在父会话中它将为空)
With your model, you can't. Add a parent section:
and set that for every child session (it will be null in the parent session)
我想,每个部分(主要部分除外)都有一个父级。诀窍是,一个部分需要知道它的父部分。
一种广泛使用的模式是使用构造函数设置父级,并向构造函数添加一些逻辑,以便它自动将该节注册为父级的子级:
然后使用以下代码添加一个节:
重构技巧 -将所有字段声明为私有并实现 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:
Then use this code to add a section:
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.