对象比较
class Book{
private String title;
private double price;
public Book(String title,double price) {
this.title=title;
this.price=price;
}
public boolean compare(Book book) {
if(book==null) {
return false;
}
if(book==this) {
return true;
}
if(this.title.equals(book.title)&&this.price==book.price) {
return true;
}
else {
return false;
}
}
}
public class Demo{
public static void main(String args[]) {
Book b1=new Book("java",123.90);
Book b2=new Book("java",123.90);
if(b1.compare(b2)) {
System.out.println("是同一个对象");
}
else {
System.out.println("不是同一个对象");
}
}
}