类定义

实验内容及要求

  • 设计并实现一个Book类,定义义成Book.java,Book 包含书名,作者,出版社和出版日期,这些数据都要定义getter和setter。定义至少三个构造方法,接收并初始化这些数据。覆盖(Override)toString方法,返回良好的含有多行的书的描述信息。覆盖equals方法,书名,作者,出版社和出版日期完全一致才说明两本书是一样的。

  • 创建一个测试类Bookshelf, 其中的main方法创建并更新几个Book对象。Book至少包含三本本学期教材内容。

实验过程

  • 产品代码
public class Book {
    private String bookname;
    private String author;
    private String publishing;
    private String price;

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getPublishing() {
        return publishing;
    }

    public void setPublishing(String publishing) {
        this.publishing = publishing;
    }

    public String getPrice() {
        return price;
    }

    public void setprice(String price) {
        this.price = price;
    }

    public String getBookname() {

        return bookname;
    }

    public void setBookname(String bookname) {
        this.bookname = bookname;
    }

    public Book(String bookname, String author, String publishing, String price) {
        this.bookname = bookname;
        this.author = author;
        this.publishing = publishing;
        this.price = price;
    }

    @Override
    public String toString() {
        return "Book{" +
                "bookname='" + bookname + '\'' +
                ", author='" + author + '\'' +
                ", publishing='" + publishing + '\'' +
                ", price='" + price + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Book book = (Book) o;

        if (bookname != null ? !bookname.equals(book.bookname) : book.bookname != null) return false;
        if (author != null ? !author.equals(book.author) : book.author != null) return false;
        if (publishing != null ? !publishing.equals(book.publishing) : book.publishing != null) return false;
        return price != null ? price.equals(book.price) : book.price == null;
    }

    @Override
    public int hashCode() {
        int result = bookname != null ? bookname.hashCode() : 0;
        result = 31 * result + (author != null ? author.hashCode() : 0);
        result = 31 * result + (publishing != null ? publishing.hashCode() : 0);
        result = 31 * result + (price != null ? price.hashCode() : 0);
        return result;
    }
  • 测试代码
public class Bookshelf {
    public static void main(String[] args){
        Book b = new Book("Java","林信良","清华大学出版社","68");
        Book b1 = new Book("Java学习笔记","林信良","清华大学出版社","68");
        System.out.println(b.getBookname());
        System.out.println(b.equals(b1));
        b.setPublishing("java学习笔记");
        System.out.println(b.equals(b1));
        System.out.println(b.getBookname());
        System.out.println(b.getAuthor());
        System.out.println(b.getAuthor());
        System.out.println(b.getPublishing());
        System.out.println(b.getPrice());
    }
}
  • 实验截图

实验总结

  • 今后应多加练习,争取更加熟练的掌握,在课上能及时提交。