[英]How to deep copy an object from another class?
I have another class Author that returns the first and last name of an author and their email address.我有另一个 class Author 返回作者的名字和姓氏及其 email 地址。 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.我以为我可以只使用我的 getAuthor 方法,但它不起作用。
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;
}
}
}
Your Author field has the Object
type:您的作者字段的类型为Object
:
private Object Author;
You need to change Object
to Author
.您需要将Object
更改为Author
。
Also, consider renaming the field from Author
to author
because it may be confused with the Author
class.此外,考虑将字段从Author
重命名为author
,因为它可能与Author
class 混淆。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.