我正在尝试将所有类型为“平装本”的书籍打印到控制台。但我得到的问题是它说变量类型不在课本中。我想这是因为对象存储在一个书对象数组中,但我需要将所有书籍存储在一个数组中。如果有人建议如何更好地将对象存储在一个数组中,或者我如何访问我需要的变量,那将非常感激。
这是驱动程序类Test
public class Test {
public static void main(String[] args) {
BookCollection bc = new BookCollection();
bc.addNewBook("Big Java", "Horstmann", 2014, 150.0,"Paperback");
bc.addUsedBook("Java", "Deitel", 1999, 120.0, 0.75);
bc.addNewBook("JavaScript", "Hoque", 2005, 80.50, "Hardcover");
bc.addNewBook("C++", "Smith", 2004, 135.0, "Paperback");
bc.addUsedBook("C", "Jones", 2000, 110.0, 0.6);
System.out.println(bc.printReport());
System.out.println("****************************************");
//System.out.println(bc.printAllBooksWithSellingPriceBelow(100.0));
//System.out.println("****************************************");
System.out.println(bc.printAllPaperbackBooks());
}
} // Hello
结尾这是Book类
class Book {
public String title;
public String author;
public int year;
public double price;
public double usageIndex;
public Book(String t, String a, int y, double p) {
title = t;
author = a;
year = y;
price = p;
} // This creates the parent class book with the default attributes
public String toString() {
return title + author + year + price;
} // This puts anything the book class contains into a string form
}// end of Book
以下是NewBook和UsedBook这两个扩展书
的类class BookCollection {
private Book[] books;
private String pT;
private String pA;
private int pY;
private double pP;
private String pTy;
private double pI;
private int i = 0;
private int count = 0;
private String report = "";
private String below = "";
private double pr;
private String paper = "";
public BookCollection() {
books = new Book[1000];
}
public void addNewBook(String pTitle, String pAuthor, int pYear, double pPrice, String pType) {
pT = pTitle;
pA = pAuthor;
pY = pYear;
pP = pPrice;
pTy = pType;
books[i++] = new NewBook(pT, pA, pY, pP, pTy);
count++;
}
public void addUsedBook(String pTitle, String pAuthor, int pYear, double pPrice, double pIndex) {
pT = pTitle;
pA = pAuthor;
pY = pYear;
pP = pPrice;
pI = pIndex;
books[i++] = new UsedBook(pT, pA, pY, pP, pI);
count++;
}
public String printReport() {
for (int j = 0; j < count; j++) {
if (books[j] instanceof UsedBook) {
report = report.concat("Books " + (j + 1) + ": Used Book\n" + books[j].toString());
} // if object is UsedBook, then say so
if (books[j] instanceof NewBook) {
report = report.concat("Books " + (j + 1) + ": New Book\n" + books[j].toString());
} // if object is NewBook, then say so
}
return report;
} // end of printReport()
public String printAllPaperbackBooks() {
for (int z = 0; z < count; z++) {
if (books[z] instanceof NewBook) {
if (books[z].type.equalsIgnoreCase("Paperback")) {
paper = paper.concat("Books " + (z + 1) + ": New Book\n" + books[z].toString());
}
} // if object is NewBook, then say so
}
return paper;
}
}
这是我得到的错误
Test.java:139: error: cannot find symbol
if (books[z].type.equalsIgnoreCase("Paperback")) {
^
symbol: variable type
location: class Book
1 error
答案 0 :(得分:0)
发生此问题是因为type
不是Book
中的公共字段。
假设type
是名为NewBook
的类中的公共字段(您尚未向我们展示),则需要将books[z]
投射到NewBook
:
if (((NewBook)books[z]).type.equalsIgnoreCase("Paperback")) {