我是OOP和Java Classes的新手,所以请耐心等待。
我正在尝试创建一个类,允许我存储有关书籍的数据,特别是字段:标题,作者,出版年份,页数,流派
然后在该课程中,我希望有一个运行的主程序,要求输入许多书籍,然后允许用户输入每本书的信息。然后,为每本书打印摘要。
我试图编写一个班级" 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);
}
}
我有多个问题/问题:
在主类中创建数组会更好吗?
我觉得我的效率非常低,因为感觉我一遍又一遍地在代码中重复自己。一些冗余对于良好实践是必要的(例如吸气剂和制定者),但我是否过度了?
在主类的for循环中,我想创建多个对象并将它们添加到数组中,但我不确定如何执行此操作,因为它需要不同的名称以避免被覆盖。
此外,在我的主类结束时调用getBookCount不起作用。有这个原因吗?
我很感激您可能会注意到的任何一般性建议或其他事项,因为我觉得我从根本上误解了Java类的使用。
答案 0 :(得分:2)
您正在学习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
无效,因为您调用了在book
循环中定义的未定义参数for
。如果需要,您应该定义静态getter方法public static int getBookCount()
,并访问它BookClass.getBookCount()
。但我建议你不要用它来计算对象,因为当你删除对象时它会在复杂的程序中出错,使用多线程同步...