public class Main {
public static void main(String[] args) {
Book b = new Book("三国演绎",20);
Book b1 = new Book("三国演绎",20);
System.out.println(b);
System.out.println(b.equals(b1)); //yes
System.out.println(b.equals("hahh")); //no
}
}
class Book{
private String title;
private double price;
public Book(String title,double price){
this.title = title;
this.price = price;
}
public String toString(){
return super.toString() + "\n" + "书名:" + this.title + ",价格:" + this.price;
}
@Override
public boolean equals(Object obj) {
if(this==obj){
return true;
}
if(obj == null){
return false;
}
if(!(obj instanceof Book)){
return false;
}
Book book = (Book)obj;
if(this.title.equals(book.title)&&this.price==book.price){
return true;
}
return false;
}
}