public String toString(){ return str; }
public boolean equals(Object obj) { return (this == obj); }
重写if (this == obj)
判断对象地址是否相等,如果是就不用判断,提高效率if (obj == null)
对象若为空,则不往下进行f (getClass() != obj.getClass())
判断两个对象是否一样Book bk = (Book)obj
向下转型public class bookShelf {
public static void main(String[] args) {
Book b1 = new Book("Java实用教程","耿祥义","清华大学出版社","2017年9月");
Book b2 = new Book("密码学","郑秀林","金城出版社","2016年8月");
Book b3 = new Book("汇编语言程序设计","钱晓捷","中国工信出版社","2018年6月");
System.out.println(b1.getBookname());
System.out.println(b2.getDate());
System.out.println(b3.getPublisher());
System.out.println(b1.equals(b2));
System.out.println(b2.equals(b2));
}
}
原文:https://www.cnblogs.com/orii/p/10662991.html