gpt4 book ai didi

java - 面向对象编程的问题,创建多个实例

转载 作者:行者123 更新时间:2023-12-02 02:30:38 26 4
gpt4 key购买 nike

我是 OOP 和 Java 类的新手,所以请耐心等待。

我正在尝试创建一个类,允许我存储有关书籍的数据,特别是字段:标题、作者、出版年份、页数、流派

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

我尝试编写一个类“bookClass”,允许输入书籍信息,然后编写一个类“bookTest”,使用 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 中的静态属性来存储书籍数量,您有很多方法可以做到这一点。

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

关于java - 面向对象编程的问题,创建多个实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47171061/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com