引数なしのコンストラクタを使用して配列に追加する方法についてはわかりません。私がここで紛失しているのは何ですか?引数なしのコンストラクタを使用した配列への追加
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;
}
ここで私は多くの問題を抱えています。私は主に私の持っている、私は私の主催で配列を追加する方法を把握することはできません私の引数なしのコンストラクタがあります。適切な方向へのアドバイスやポインターは素晴らしいでしょう。
}
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);
}
}
はところで、 'super'を呼び出す必要はありません。それは暗黙のうちに行われます。 – Li357
それでは、まさに問題は何ですか? – Mureinik
最後の2冊の本をアレイに追加できないようです。配列は、追加しようとしている2つの書籍の代わりにnullを出力します。 – Boomboxcity