我似乎被困在如何使用no-arg构造函数添加到数组中。我在这里失踪的是什么?
public class Book {
private String title;
private String author;
private int pages;
private double price;
public static int numBooks = 0;
public Book(String title, String author, int pages, double price) {
super();
this.title = title;
this.author = author;
this.pages = pages;
this.price = price;
numBooks++;
}
public Book() {
super();
numBooks++;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getPages() {
return pages;
}
public void setPages(int pages) {
this.pages = pages;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "book [title=" + title + ", author=" + author + ", pages=" + pages + ", price=" + price + "]";
}
public static int getNumBooks(){
return numBooks;
}
这是我遇到很多麻烦的地方。我有我的主要和它下面我有我的无arg构造函数,我无法弄清楚如何在main中添加数组。任何正确方向的建议或指示都会很棒。
}
public class TestBook {
private String title;
private String author;
private int pages;
private double price;
public TestBook(String title, String author, int pages, double price){
this.title = title;
this.author = author;
this.pages = pages;
this.price = price;
}
public static void main(String[] args) {
Book[] bookArray = new Book[6]; //This is the array that I am trying to add the last two books too
Book bookArray0 = new Book("Java Proramming", "Liang", 1320, 145.00);
Book bookArray1 = new Book("Horton Hears a Who", "Dr. Seuss", 72, 19.99);
Book bookArray2 = new Book("The Hobbit", "Tolkien", 320, 9.25);
Book bookArray3 = new Book("Born a Crime", "Noah", 304, 17.33);
Book bookArray4 = new Book();
Book bookArray5 = new Book();
bookArray[0] = bookArray0;
bookArray[1] = bookArray1;
bookArray[2] = bookArray2;
bookArray[3] = bookArray3;
bookArray[4] = bookArray4;
bookArray[5] = bookArray5;
for(Book d : bookArray)
System.out.println(d);
}
public TestBook() { //This is the no-arg that I am trying to use to add to the array above.
Book[] bookArray = new Book[6];
Book bookArray4 = new Book("The Town", "Chuck Hogan", 477, 14.99);
Book bookArray5 = new Book("The Pretender ", "Sombody", 400, 24.99);
bookArray[4] = bookArray4;
bookArray[5] = bookArray5;
for(Book b : bookArray)
System.out.println(b);
}
public void finishArray(){
Book lastBook = new Book();
lastBook.setTitle(title);
lastBook.setAuthor(author);
lastBook.setPages(pages);
lastBook.setPrice(price);
}
}