• 20175312 2018-2019-2 《Java程序设计》第6周课下选做——类定义


    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;
    }
    

    }

    运行结果截图

    码云链接

  • 相关阅读:
    第十章 泛型程序设计与C++标准模板库 迭代器
    Linux 命令
    Linux 命令
    Linux 命令
    Linux 命令
    Linux 命令
    Linux 命令
    Linux 命令
    Linux 命令
    Linux 命令
  • 原文地址:https://www.cnblogs.com/20175312-tgy/p/10664572.html
Copyright © 2020-2023  润新知