class Book{
private String title;
private double price;
public Book(String title) {
this.title=title;
}
public Book(String title,double price){
this.title = title;
this.price = price;
}
public String toString(){
return "商品名字"+title+"\n"+"价格"+price;
}
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;
}
else {
return false;
}
}
}
public class TestDemo{
public static void main(String args[]) {
Book book1=new Book("小团圆",13);
Book book2=new Book("小团圆");
Book book3=new Book("小团圆",13);
System.out.println(book1.equals(book2));
System.out.println(book1.equals(book3));
}
}