java中数组可以存储对象
public class Test14 {
public static void main(String[] args) {
Book[] book = new Book[3];
book[0] = new Book("java编程思想","Bruce Eckel",108);
book[1] = new Book("菜根谭","洪应明",58);
book[2] = new Book("百年孤独","加西亚·马尔克斯",108);
for(int i=0;i<book.length;i++){
System.out.println(book[i].getName()+"\t"+book[i].getAuthor()+"\t"+book[i].getPrice());
}
}
}
class Book{
private String name;
private String author;
private double price;
public Book(){
}
public Book(String name, String author, double price) {
this.name = name;
this.author = author;
this.price = price;
}
public String toString() {
return "Book [author=" + author + ", name=" + name + ", price=" + price
+ "]";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
温馨提示:答案为网友推荐,仅供参考