在比较两个数字是否相等的时候,可以使用“==”来操作,比较两个字符串是否相等可以使用“equals()”来操作,如果说现在有一个自定义的类,该如何来判断这个类的两个对象是否相等。对象=数据集合,实际上判断对象是否相等就是判断对象内的属性是否相等。
class Book {
private String title;
private double price;
public Book(String title, double price) {
this.title = title;
this.price = price;
}
public String getTitle() {
return this.title;
}
public double getPrice() {
return this.price;
}
}
public class MainClass {
public static void main(String[] args) {
Book b1 = new Book("Java基础入门", 79.8);
Book b2 = new Book("Java基础入门", 79.8);
if (b1.getTitle().equals(b2.getTitle())
&& b1.getPrice() == b2.getPrice()) {
System.out.println("是同一个对象");
} else {
System.out.println("不是同一个对象");
}
}
}
class Book {
private String title;
private double price;
public Book(String title, double price) {
this.title = title;
this.price = price;
}
public String getTitle() {
return this.title;
}
public double getPrice() {
return this.price;
}
public boolean compare(Book book) {
if (book == null) {
return false;
}
if (this == book) {
return true;
}
if (this.getTitle().equals(book.getTitle())
&& this.getPrice() == book.getPrice()) {
return true;
} else {
return false;
}
}
}
public class MainClass {
public static void main(String[] args) {
Book b1 = new Book("Java基础入门", 79.8);
Book b2 = new Book("Java基础入门", 79.8);
if (b1.compare(b2)) {
System.out.println("是同一个对象");
} else {
System.out.println("不是同一个对象");
}
}
}