我是OOP和Java类的新手,所以请多多包涵。

我正在尝试创建一个类,该类允许我存储有关书籍的数据,尤其是以下字段:书名,作者,出版年份,页数,类型

然后,在该课程中,我希望有一个运行的主程序,要求输入许多书籍,然后允许用户输入每本书的信息。然后,为每本书打印摘要。

我试图编程一个类“ bookClass”,该类允许输入书籍信息,然后尝试一个类“ bookTest”,该类使用bookClass创建一个数组。

这是bookClass:

public class bookClass {
    private String title;
    private String author;
    private String year;
    private String pageCount;
    private String genre;
    private static int bookCount;

    public bookClass(String title, String author, String year, String pageCount, String genre) {
        this.setTitle(title);
        this.setAuthor(author);
        this.setYear(year);
        this.setPageCount(pageCount);
        this.setGenre(genre);
        bookCount++;
    }

    public void setTitle(String title) {
        this.title = title;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    public void setYear(String year) {
        this.year = year;
    }
    public void setPageCount(String pageCount) {
        this.pageCount = pageCount;
    }
    public void setGenre(String genre) {
        this.genre = genre;
    }

    public String getTitle() {
        return title;
    }

    public String getAuthor() {
        return author;
    }

    public String getYear() {
        return year;
    }

    public String getPageCount() {
        return pageCount;
    }

    public String getGenre() {
        return genre;
    }

    public int getBookCount() {
        return bookCount
    }
}


这是主要的类:

import java.util.*;

public class bookTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.print("How many books would you like to enter: ");
        int bookAmount = input.nextInt(); input.nextLine();
        String[][] books2DArray = new String[bookAmount][5];
        for (String[] books1DArray: books2DArray) {
            System.out.print("Please enter the title: ");
            String title = input.nextLine();
            System.out.print("Please enter the author ");
            String author = input.nextLine();
            System.out.print("Please enter the year: ");
            String year = input.nextLine();
            System.out.print("Please enter the page count: ");
            String pageCount = input.nextLine();
            System.out.print("Please enter the genre: ");
            String genre = input.nextLine();
            bookClass book = new bookClass(title, author, year, pageCount, genre);
            books1DArray[0] = book.getTitle();
            books1DArray[1] = book.getAuthor();
            books1DArray[2] = book.getYear();
            books1DArray[3] = book.getPageCount();
            books1DArray[4] = book.getGenre();

        }
        for (String[] books1DArray: books2DArray) {
            printSummary( books1DArray[0], books1DArray[1], books1DArray[2], books1DArray[3], books1DArray[4]);
        }
        System.out.println("There are" + book.getBookCount() + "books in the system");
    }

    public static void printSummary(String title, String author, String year, String pageCount, String genre) {
        System.out.format("%s written by %s was published in %s, has %s pages and is of the %s genre", title, author, year, pageCount, genre);

    }

}


我有多个问题/疑问:

在主类中创建数组会更好吗?

我觉得自己效率很低,就像我在代码中重复一遍又一遍。某些冗余对于良好实践是必不可少的(例如getter和setter),但是我是否过度使用它?

在主类的for循环中,我想创建多个对象并将它们添加到数组中,但是我不确定如何执行此操作,因为它将需要使用不同的名称以避免被覆盖。

另外,在主类末尾调用getBookCount无效。是否有一个原因?

我将不胜感激,您可能会注意到任何一般性建议或其他事项,因为我似乎从根本上误解了Java类的使用。

最佳答案

您正在学习Java,因此您应该学习Java name conventions

我假设您的书类为public class BookClass,您可以编写使用1个BookClass(或ArrayList或所需的任何集合类型)数组的测试类,例如(跳过输入检查)

public class BookTest {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        System.out.print("How many books would you like to enter: ");
        int bookAmount = input.nextInt(); input.nextLine();
        BookClass[] books = new BookClass[bookAmount];

        for (int i = 0; i < bookAmount; i++) {
            System.out.print("Please enter the title: ");
            String title = input.nextLine();
            System.out.print("Please enter the author ");
            String author = input.nextLine();
            System.out.print("Please enter the year: ");
            String year = input.nextLine();
            System.out.print("Please enter the page count: ");
            String pageCount = input.nextLine();
            System.out.print("Please enter the genre: ");
            String genre = input.nextLine();
            books[i] = new BookClass(title, author, year, pageCount, genre);
        }
        for (BookClass book : books) {
            printSummary(book.getTitle(), book.getAuthor(), book.getYear(), book.getPageCount(), book.getGenre());
        }
        System.out.println("There are" + books.length + "books in the system");
    }

    public static void printSummary(String title, String author, String year, String pageCount, String genre) {
        System.out.format("%s written by %s was published in %s, has %s pages and is of the %s genre", title, author, year, pageCount, genre);
    }

}


您不需要BookClass中的static属性来存储书籍数量,您可以通过多种方法来实现。

您的getBookCount不起作用,因为您调用了在侧面book循环中定义的未定义参数for。如果需要,您应该定义一个静态的getter方法public static int getBookCount(),并访问它BookClass.getBookCount()。但是我建议您不要使用它来计数对象,因为删除对象时,在复杂的程序中它可能会出错,请使用多线程而不同步...

10-06 11:12