有个小小的java问题,请问在java中数组可以储存对象吗??如果可以储存的话,那可以再调用储存在

有个小小的java问题,请问在java中数组可以储存对象吗??如果可以储存的话,那可以再调用储存在数组里面的对象的成员属性?

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;
}

}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2020-03-17
可以存呀,取得话,直接用for循环遍历 出来该数组 就可以 得到每一个对象了
相似回答