如何正确初始化Java中的列表成员对象 [英] How to correctly initialize a list member object in Java

查看:330
本文介绍了如何正确初始化Java中的列表成员对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个类:

public class Book extends SugarRecord {
    private Long id;
    private String mBookName;
    private String mAuthorName;
    private List<Page> mPageList;

    public Book() {

    }

    public Book(String bookname, String authorName) {
        mBookName = bookname;
        mAuthorName = authorName;
        mPageList = new ArrayList<>();
    }

    public Book(String bookname, String authorName, List<Page> pageList) {
        mBookName = bookname;
        mAuthorName = authorName;
        mPageList = pageList;
    }

    @Override
    public Long getId() {
        return id;
    }

    @Override
    public void setId(Long id) {
        this.id = id;
    }

    public String getAuthorName() {
        return mAuthorName;
    }

    public void setAuthorName(String authorName) {
        mAuthorName = authorName;
    }

    public String getBookName() {
        return mBookName;
    }

    public void setBookName(String bookName) {
        mBookName = bookName;
    }

}

但只是为了:

public class Page {
    private Long id;
    private String mText;

    public Page() {

    }
    public Page(String text) {
        mText = text;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getText() {
        return mText;
    }

    public void setText(String text) {
        mText = text;
    }
}

现在我认为有两个构造函数,一个如果你有页面已经和一个,如果你不,但这是正确的方式去吗?

Right now I figure it makes sense to have two constructors, one for if you have pages already and one if you don't, but is this the right way to go about it? Or do I need to copy the ArrayList that comes into the constructor as opposed to merely referencing it?

推荐答案

第一个构造函数:

public Book(String bookname, String authorName) {
    mBookName = bookname;
    mAuthorName = authorName;
    mPageList = new ArrayList<>();
}

然后您将有一本没有任何页面的新书

Then you will have a new book without any page

第二个构造函数:

public Book(String bookname, String authorName, List<Page> pageList) {
    mBookName = bookname;
    mAuthorName = authorName;
    mPageList = pageList;
}

您将有一本新书,页面<

You will have a new book with pages referenced to the pages (Might be in DB).

由于java中的arraylist是muttable,任何更改的数据都将被修改为原始数据(参见 Java Immutable Collections

Since the arraylist in java is muttable , any changed data will be modified to the original data (See here Java Immutable Collections)

如果您打算使用数据没有任何改变的原始数据(Just copy),你可能应该使用immutable集合,以避免这个问题。但是如果你要使用它与一些修改(我看到类是扩展SugarRecord),第二个构造函数将是你的好。

If you are going to used the data without any change to the origin data(Just copy) , you might should use the immutable collection to avoid this problem. But if you are going to used it with some modification ( I saw the class is extended SugarRecord), the second constructor will be alright for you.

这篇关于如何正确初始化Java中的列表成员对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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