我正在编写一个包含“ Book”抽象类的程序,我有两个从“ Book”继承的类(“ LearnBook”和“ ReadingBook”)。
图书:
Public abstract class Book {
protected String name;
protected String author;
LearningBook:
public class LearningBook extends Book {
private String subject;
ReadingBook:
public class ReadingBook extends Book {
private int numberOfPages;
在主类中,我具有Book数组,该数组可以包含Book的任何实例。
我想添加一个方法来检查两个Book对象是否完全相同,以防止在Book数组中重复。看起来像这样:
public boolean sameBookCheck(Book book1, Book book2)
所以我的第一个想法是在Book类中编写一个isEqual()方法,该方法检查“名称”和“作者”是否相等。 但是然后我需要检查这是一本学习书还是一本阅读书,这样我才能知道我是否需要比较“主题”值或“ numberOfPage”值。
我不知道该怎么做,感谢您的帮助。
答案 0 :(得分:2)
您可以使用以下设计:
Book
中,抽象类具有equals()
函数,并检查另一个对象是否为Book
类型,并且在所有字段中都具有相同的值。LearningBook
和ReadingBook
中具有equals()
函数,该函数首先检查另一个对象是否属于同一类,然后调用Book's equals()
函数,检查抽象类的字段,然后检查当前类别的字段是否具有相同的值。看看代码:
abstract class Book {
protected String name;
protected String author;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((author == null) ? 0 : author.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Book))
return false;
Book other = (Book) obj;
if (author == null) {
if (other.author != null)
return false;
} else if (!author.equals(other.author))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
class LearningBook extends Book{
private String subject;
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + ((subject == null) ? 0 : subject.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (!(obj instanceof LearningBook))
return false;
LearningBook other = (LearningBook) obj;
if (subject == null) {
if (other.subject != null)
return false;
} else if (!subject.equals(other.subject))
return false;
return true;
}
}
class ReadingBook extends Book{
private int numberOfPages;
public int getNumberOfPages() {
return numberOfPages;
}
public void setNumberOfPages(int numberOfPages) {
this.numberOfPages = numberOfPages;
}
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + numberOfPages;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (!(obj instanceof ReadingBook))
return false;
ReadingBook other = (ReadingBook) obj;
if (numberOfPages != other.numberOfPages)
return false;
return true;
}
}
public class Runner {
public static void main(String[] args) {
Book learningBook = new LearningBook();
learningBook.setAuthor("auth");
learningBook.setName("sci");
Book learningBook2 = new LearningBook();
learningBook2.setAuthor("auth");
learningBook2.setName("sci");
Book readingBook = new ReadingBook();
readingBook.setAuthor("auth");
readingBook.setName("sci");
//returns false
System.out.println(learningBook.equals(readingBook) );
//returns true
System.out.println(learningBook.equals(learningBook2) );
}
}
答案 1 :(得分:1)
您可以使用instanceof
方法来比较Object
的类型。检查它是LearningBook
还是ReadingBook
example
回答您的评论,
让我们说,当您检查两个实例时会说它们是不同的,那么就没有问题可以返回false。但是,如果实例也相同,那么之后您可以使用类似的方法进行检查
if (both instances are same) {
if (yourObjectIs instanceof LearningBook) {
you can check the two values of LearningBook here and return true if the are equals
} else {
you can check the two values of ReadingBook here and return true if the are equals
}
}
答案 2 :(得分:1)
为三个类的每一个编写一个equals-implementation。每个实现仅负责其自己的字段。
子类ReadingBook
和LearningBook
的equals-implementation应该在某个地方调用super.equals()
-Book
的equals-implementation。
答案 3 :(得分:1)
您可以向图书实例询问其类,并检查类的相等性。
book1.getClass().equals(book2.getClass())
答案 4 :(得分:1)
如前所述,您应该覆盖equals(Object object)方法。在您的示例中,您可以这样操作:
public abstract class Book{
@NonNull protected String name;
@NonNull protected String author;
public Book(String name, String author) {
this.name = name;
this.author = author;
}
@Override
public boolean equals(Object object) {
if (object instanceof Book) {
var book = (Book) object;
return this.name.equals(book.name) && this.author.equals(book.author);
} else
return false;
}
}
public class LearningBook extends Book{
@NonNull private String subject;
public LearningBook(String name, String author,String subject) {
super(name, author);
this.subject = subject;
}
@Override
public boolean equals(Object object) {
if (object instanceof LearningBook) {
var book = (LearningBook) object;
return this.subject.equals(book.subject) && super.equals(book);
} else
return false;
}
}
public class ReadingBook extends Book{
@NonNull private int numberOfPages;
public ReadingBook(String name, String author,int numberOfPages) {
super(name, author);
this.numberOfPages = numberOfPages;
}
@Override
public boolean equals(Object object) {
if (object instanceof ReadingBook) {
var book = (ReadingBook) object;
return super.equals(book) && this.numberOfPages == book.numberOfPages;
} else
return false;
}
}
我已经使用@NonNull注释来避免在equals方法中使用NPE。