我似乎在如何使用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;
}
这是我遇到很多麻烦的地方。我的主要位置,下面有我的No 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);
}
}
您的收入为null,因为您没有将数据设置为这些对象。这就是settitle和setauthor的目的。所以
之类的东西 bookArray4.setTitle("The Town");
bookArray4.setAuthor("Somebody");
当您使用传递信息的构造函数时,这些内容已设置在其中。但是对于没有参数的构造函数,没有传递数据。因此,您使用"设置器"方法设置数据
as sean reaves 指出,您需要将固定器用于使用默认构造函数创建的书籍。
书
public class Book {
private String title;
private String author;
private int pages;
private double price;
private static int numBooks = 0;
public Book(String title, String author, int pages, double price) {
this();
this.title = title;
this.author = author;
this.pages = pages;
this.price = price;
}
public Book() {
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=" + this.title + ", author=" + this.author + ", pages=" + this.pages + ", price=" + this.price + "]";
}
public static int getNumBooks() {
return numBooks;
}
}
我对您的代码进行了一些小修改,只是为了清理一点。
MAIN
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
// All books
Book[] books = new Book[] {
new Book("Java Proramming", "Liang", 1320, 145.00),
new Book("Horton Hears a Who", "Dr. Seuss", 72, 19.99),
new Book("The Hobbit", "Tolkien", 320, 9.25),
new Book("Born a Crime", "Noah", 304, 17.33),
new Book(),
new Book()
};
// Use setters to avoid null values
books[5].setAuthor("Not Null Author");
books[5].setTitle("Not Null Title");
// Print
Arrays.asList(books).forEach(System.out::println);
}
}
您也可以将price
和pages
属性的固定器拨打。
输出
book [title=Java Proramming, author=Liang, pages=1320, price=145.0]
book [title=Horton Hears a Who, author=Dr. Seuss, pages=72, price=19.99]
book [title=The Hobbit, author=Tolkien, pages=320, price=9.25]
book [title=Born a Crime, author=Noah, pages=304, price=17.33]
book [title=null, author=null, pages=0, price=0.0]
book [title=Not Null Title, author=Not Null Author, pages=0, price=0.0]