Java 面向对象编程的问题,创建多个实例
我不熟悉OOP和Java类,请耐心听我说 我试图创建一个类,允许我存储有关书籍的数据,特别是字段:标题、作者、出版年份、页数、流派 然后在这个类中,我希望有一个运行的主程序,要求输入一些书,然后允许用户输入每本书的信息。然后,为每本书打印一份摘要 我试图编写一个类“bookClass”,允许输入图书信息,然后编写一个类“bookTest”,使用bookClass创建一个数组 这是图书班:Java 面向对象编程的问题,创建多个实例,java,oop,Java,Oop,我不熟悉OOP和Java类,请耐心听我说 我试图创建一个类,允许我存储有关书籍的数据,特别是字段:标题、作者、出版年份、页数、流派 然后在这个类中,我希望有一个运行的主程序,要求输入一些书,然后允许用户输入每本书的信息。然后,为每本书打印一份摘要 我试图编写一个类“bookClass”,允许输入图书信息,然后编写一个类“bookTest”,使用bookClass创建一个数组 这是图书班: public class bookClass { private String title;
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 我假设您的book类
公共类BookClass
,您可以编写使用一个BookClass
数组(或ArrayList
或您想要的任何集合类型)的测试类,例如(跳过输入检查)
公共类图书测试{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
System.out.print(“您想输入多少本书:”);
int bookAmount=input.nextInt();input.nextLine();
BookClass[]books=新的BookClass[账面金额];
对于(int i=0;i
您不需要在BookClass
中使用static属性来存储图书数量,您可以使用多种方法来存储图书数量
您的
getBookCount
不起作用,因为您调用了一个未定义的参数book
,该参数是您在side中为
循环定义的。如果需要,应该定义一个静态getter方法publicstaticintgetbookcount()
,并访问它BookClass.getBookCount()
。但我建议你们不要用它来计算对象,因为当你们移除对象时,在复杂的程序中它会出错,使用多线程而不同步…你们在学习Java,所以你们应该学习Java
我假设您的book类公共类BookClass
,您可以编写使用一个BookClass
数组(或ArrayList
或您想要的任何集合类型)的测试类,例如(跳过输入检查)
公共类图书测试{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
System.out.print(“您想输入多少本书:”);
int bookAmount=input.nextInt();input.nextLine();
BookClass[]books=新的BookClass[账面金额];
对于(int i=0;i
您不需要在BookClass
中使用static属性来存储图书数量,您可以使用多种方法来存储图书数量
您的getBookCount
不起作用,因为您调用了一个未定义的参数book
,该参数是您在side中为
循环定义的。如果需要,您应该定义
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);
}
}