我创建了自己的名为Property的类,并对其进行了实例化。现在我想打印出它的实例之一到控制台,但事已被打印出来,已经显示出这样的信息: 为什么我无法将自己的类的对象打印到Java中的控制台中
,这是我的代码:
package playground;
public class PlayGround{
public static void main(String[] args) {
Property a = new Property("house",120.2,1989);
System.out.println(a);
}
}
package playground;
public class Property {
private String type;
private double area;
private int year;
public Property(String type, double area, int year){
this.type=type;
this.area=area;
this.year=year;
}
}
覆盖了'toString'方法与你的类的字符串表示。 – Henrik
你的意思是我必须在我的类中创建一个名为toString()的方法吗? –
是的,但你应该看看从这里链接到的问题。他们会为您提供所需的背景和答案。 – Henrik