Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
465 views
in Technique[技术] by (71.8m points)

android - How to correctly initialize a list member object in Java

I have this class:

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;
    }

}

The Page class isn't much but just in case:

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The first constructor:

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

Then you will have a new book without any page

The second constructor:

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).

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...