标签:实现 lse 步骤 判断 public 设计 pac 不难 obj
设计并实现一个Book类,定义义成Book.java,Book 包含书名,作者,出版社和出版日期,这些数据都要定义getter和setter。定义至少三个构造方法,接收并初始化这些数据。覆盖(Override)toString方法,返回良好的含有多行的书的描述信息。覆盖equals方法,书名,作者,出版社和出版日期完全一致才说明两本书是一样的。
创建一个测试类Bookshelf, 其中的main方法创建并更新几个Book对象。Book至少包含三本本学期教材内容。
之后又去搜索了一下equals()方法,并百度了相关的重写方法,equals()方法的重写操作步骤比较多
if(this==obj)
if(obj==null)
if(getClass() != obj.getClass())
if(!(obj instanceof Book))
public int hashCode() {???
return Objects.hash(name,author,publish,date);
}
package PublicDefinition;
import java.util.Objects;
public class Book {???
String name;???
String author;???
String publish;???
String date;???
Book(String name,String author) {}???
Book(String name,String author,String publish) {}???
Book(String name,String author,String publish,String date) {
this.name = name;???????
this.author = author;???????
this.publish = publish;???????
this.date = date;???
}???
public void setName(String name) {???????
this.name = name;???
}???
public void setAuthor(String author) {???????
this.author = author;???
}???
public void setPublish(String publish) {???????
this.publish = publish;???
}???
public void setDate(String date) {???????
this.date = date;???
}???
public String getName() {???????
return name;???
}???
public String getAuthor() {???????
return author;???
}???
public String getPublish() {???????
return publish;???
}???
public String getDate() {???????
return date;???
}???
public String toString() {???????
return "书名为"+name+" 作者是"+author+" 出版社为"+publish+" 出版日期是"+date;???
}???
public boolean equals(Object obj) {???????
if(this == obj) {???????????
return true;???????
}???????
if(obj == null) {???????????
return false;???????
}???????
if(getClass() != obj.getClass()) {???????????
return false;???????
}???????
if(!(obj instanceof Book)){???????????
return false;???????
}???????
else {???????????
Book book = (Book)obj;???????????
return
Objects.equals(name,book.name)&&Objects.equals(author,book.author)&&Objects.equals(publish,book.publish)&&Objects.equals(date,book.date);???????
}???
}???
public int hashCode() {???????
return Objects.hash(name,author,publish,date);???
}
}
package PublicDefinition;
public class Bookshelf {???
public static void main(String[] args) {???????
Book b1 = new Book("Java2实用教程(第五版)","耿祥义","清华大学出版社","2017年1月");???????
Book b2 = new Book("密码学","郑秀林","金城出版社","2016年8月");???????
Book b3 = new Book("计算机网络","谢希仁","电子工业出版社","2017年1月");???????
System.out.println(b1);???????
System.out.println(b2);???????
System.out.println(b3);???????
System.out.println(b1.getDate());???????
System.out.println(b2.getPublish());???????
System.out.println(b3.getAuthor());???????
System.out.println(b1.getName());???????
System.out.println(b1.equals(b2));???????
System.out.println(b2.equals(b3));???????
System.out.println(b2.equals(b2));???
}
}
https://gitee.com/wangzihong/20175209/tree/master/src/PublicDefinition
标签:实现 lse 步骤 判断 public 设计 pac 不难 obj
原文地址:https://www.cnblogs.com/wangzihong0213/p/10667143.html