创建book类

xmq68pz9  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(329)

带有5个字段的title-string,书名。作者-字符串,这本书的作者。页数-整数,书中的页数。
出版商-字符串,这本书的出版商。年份-整数,这本书出版的年份。
有6种方法
一种构造函数,它按所列顺序接受上述字段的值作为参数。一种复制构造函数,用于复制图书对象。返回标题字段值的gettitle方法。接受字符串参数的setauthor方法,用于设置author字段。一种equals方法,返回一个布尔值,指示两个对象是否包含相同的信息。如果他们这样做了,它会返回true,否则返回false。当对象的字段包含相同的值时,对象包含相同的信息。一种tostring方法,返回一个字符串,该字符串在单独的行(5行)上包含每个字段的名称和值。用一个程序演示这个课程。演示程序需要:创建至少两个图书对象。创建图书对象的副本。使用其他四种方法中的每一种来证明它们工作正常。不要在演示程序中显式调用tostring方法。

bis0qfac

bis0qfac1#

这是教科书的代码。在 toString 方法就是这样 String.format 使字符串可读的方法。没什么比这更特别的了。什么时候 equals 方法被重写 hashCode 方法也应该是。如果它们相等,它们都比较对象。你可以从这里找到更多的例子。

public class Book {

    private String title;
    private String author;
    private int pages;
    private String publisher;
    private int year;

    public Book(String title, String author, int pages, String publisher, int year) {
        this.title = title;
        this.author = author;
        this.pages = pages;
        this.publisher = publisher;
        this.year = year;
    }

    public Book(Book book) {
        this.title = book.title;
        this.author = book.author;
        this.pages = book.pages;
        this.publisher = book.publisher;
        this.year = book.year;
    }

    public String getTitle() {
        return title;
    }

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

    @Override
    public boolean equals(Object o) {
        if (!(o instanceof Book)) {
            return false;
        }
        Book other = (Book) o;

        if (this.title.equals(other.title) &&
                this.author.equals(other.author) &&
                this.publisher.equals(other.publisher) &&
                this.pages == other.pages && this.year == other.year) {
            return true;
        }
        return false;
    }

    @Override
    public String toString() {
        return String.format("%-10s %s\n", "Title:", title) +
            String.format("%-10s %s\n", "Author:", author) +
            String.format("%-10s %d\n", "Pages:", pages) +
            String.format("%-10s %s\n", "Publisher:", publisher) +
            String.format("%-10s %d", "Year:", year);
    }

}

下面是使用所有方法的主类。

public class Main {

    public static void main(String[] args) {
        Book first = new Book("The Book", "Famous Writer", 199, "Big Book Publisher", 1984);
        Book second = new Book("The Second Book", "Less F. Writer", 249, "The Book Publishers", 1999);

        System.out.println("First book");
        System.out.println(first + "\n");
        System.out.println("Second book");
        System.out.println(second + "\n");

        System.out.println("Title of the first book: " + first.getTitle() + "\n");

        Book firstCopy = new Book(first);
        System.out.println("Copy of the first book:");
        System.out.println(firstCopy + "\n");

        System.out.println("Copy equals first book: " + firstCopy.equals(first));
        System.out.println("Second book equals first book: " + second.equals(first));
        System.out.println();

        System.out.println("Change writer of the copy to :'Most F. Writer'");
        firstCopy.setAuthor("Most F. Writer");
        System.out.println(firstCopy + "\n");
        System.out.println("Copy equals first book: " + firstCopy.equals(first));

    }

}

执行时,主类输出:

First book
Title:     The Book
Author:    Famous Writer
Pages:     199
Publisher: Big Book Publisher
Year:      1984

Second book
Title:     The Second Book
Author:    Less F. Writer
Pages:     249
Publisher: The Book Publishers
Year:      1999

Title of the first book: The Book

Copy of the first book:
Title:     The Book
Author:    Famous Writer
Pages:     199
Publisher: Big Book Publisher
Year:      1984

Copy equals first book: true
Second book equals first book: false

Change writer of the copy to :'Most F. Writer'
Title:     The Book
Author:    Most F. Writer
Pages:     199
Publisher: Big Book Publisher
Year:      1984

Copy equals first book: false

相关问题