51,411
社区成员
发帖
与我相关
我的任务
分享代码
为啥最后会去调用toString 不写的话直接输出就会是一串地址。。。。。大佬们
package USB1;
import java.util.ArrayList;
import java.util.Collection;
public class Demo000 {
public static void main(String[] args) {
Collection col = new ArrayList();
col.add(new Book("红楼梦", "曹雪芹", 23.1));
System.out.println(col);
}
}
class Book{
private String name;
private String author;
private double price;
public Book (String name , String author , double price){
this.name = name;
this.author = author;
this.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;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
'}';
}
}