如何从另一个类中深度复制对象?

huangapple go评论57阅读模式
英文:

How to deep copy an object from another class?

问题

    private String isbn;
    private String title;
    private String publisher;
    private Object Author;
    private int pages;

    public Book(String isbn, String title, Author author, String publisher, int pages) {
        this.isbn = isbn;
        this.title = title;
        this.publisher = publisher;
        this.pages = pages;
        this.Author = author;
    }

    public void setNumPages(int pageNum){
        pages = pageNum;
    }

    public String getTitle() {
        return title;
    }

    public Object getAuthor() {
        return Author;
    }

    public String getIsbn(){
        return isbn;
    }

    public String getPublisher() {
        return publisher;
    }

    public String toString(){
        return (title + ", " + getAuthor() + " (ISBN-10 #" + isbn + ", " + pages + " pages)");
    }

    public Book (Book other) {
        this(other.isbn, other.title, other.getAuthor(), other.publisher, other.pages);
    }

    public boolean equals(Object obj) {
        Book book = (Book) obj;

        if (this.isbn.equals(book.getIsbn()) && this.title.equals(book.getTitle())
                && this.getAuthor().equals(book.getAuthor())) {
            return true;
        } else {
            return false;
        }

    }
}
英文:

I have another class Author that returns the first and last name of an author and their email address. I need to do a deep copy of a book but it won't let me copy the author.

I thought I could just do use my getAuthor method but it isn't working.

Any help would be greatly appreciated. Thank you!!

    private String isbn;
    private String title;
    private String publisher;
    private Object Author;
    private int pages;

    public Book(String isbn, String title, Author author, String publisher, int pages) {
        this.isbn = isbn;
        this.title = title;
        this.publisher = publisher;
        this.pages = pages;
        this.Author = author;
    }

    public void setNumPages(int pageNum){
        pages = pageNum;
    }

    public String getTitle() {
        return title;
    }

    public Object getAuthor() {
        return Author;
    }

    public String getIsbn(){
        return isbn;
    }

    public String getPublisher() {
        return publisher;
    }

    public String toString(){
        return (title + ", " + getAuthor() + " (ISBN-10 #" + isbn + ", " + pages + " pages)");
    }

    public Book (Book other) {
        this(other.isbn, other.title, other.getAuthor(), other.publisher, other.pages);
    }

    public boolean equals(Object obj) {
        Book book = (Book) obj;

        if (this.isbn.equals(book.getIsbn()) && this.title.equals(book.getTitle())
                && this.getAuthor().equals(book.getAuthor())) {
            return true;
        } else {
            return false;
        }

    }
}

答案1

得分: 2

你的 Author 字段具有 Object 类型:

private Object 作者;

你需要将 Object 更改为 作者

此外,考虑将字段从 作者 重命名为 author,因为它可能会与 作者 类混淆。

英文:

Your Author field has the Object type:

private Object Author;

You need to change Object to Author.

Also, consider renaming the field from Author to author because it may be confused with the Author class.

huangapple
  • 本文由 发表于 2020年10月8日 07:40:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/64253760.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定