20175312 2018-2019-2 《Java程序设计》第6周课下选做——类定义
设计思路
1.我觉得Book其实就是一个中转的作用,由测试类Bookself通过Book输入数据,然后在验证通过后再取出。
2.在Book.java中储存测试类中输入的数据,然后验证信息是否一样,一样则输出,否则就返回false。
测试代码
- Bookself.java
public class Bookself {
public static void main(String[] args) {
Book book1 = new Book("Java学习笔记", "zzz", "p", 2016);
Book book2 = new Book("汇编", "xxx", "h", 2016);
Book book3 = new Book("数据结构与算法", "www", "k", 2015);
System.out.println(book1.toString());
System.out.println(book2.toString());
System.out.println(book3.toString());
}
}
- Book.java
class Book {
String name, editor, publisher;
int date;
public Book(String name, String editor, String publisher, int date) {
this.name = name;
this.editor = editor;
this.publisher = publisher;
this.date = date;
}
public void setName() {
this.name = name;
}
public void setEditor(String editor) {
this.editor = editor;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public void setDate(int date) {
this.date = date;
}
public String getName() {
return this.name;
}
public String getEditor() {
return this.editor;
}
public String getPublisher() {
return this.publisher;
}
public int getDate() {
return this.date;
}
public String toString() {
return "Book [书名=" + name + ", 作者=" + editor + ", 出版社=" + publisher + ", 出版日期=" + date + "]";
}
public boolean equals(Object obj) {
if (!(obj instanceof Book)) {
return false;
}
Book book = (Book) obj;
if (!getName().equals(book.getName())) {
return false;
}
if (!getEditor().equals(book.getEditor())) {
return false;
}
if (!getPublisher().equals(book.getPublisher())) {
return false;
}
if (!(getDate() == book.getDate())) {
return false;
}
return true;
}
}
运行结果截图
码云链接
- Bookself.java——https://gitee.com/dky20175312/dky_20175312_warehouse_1/blob/master/20175312/zuoye6/Bookself.java
- Book.java——https://gitee.com/dky20175312/dky_20175312_warehouse_1/blob/master/20175312/zuoye6/Book.java
原文地址:https://www.cnblogs.com/20175312-tgy/p/10664572.html
时间: 2024-11-05 14:44:01