Object 默认的toString 方法返回的是对象的地址,重写类的toString ()方法,然后调用就是了。
例子
package com.heima.forumAns;
public class XXXObject {
/****************成员变量和访问器***********************/
private String name;
private int age;
private String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
/***************构造方法**************/
public XXXObject(String name, int age, String address) {
super();
this.name = name;
this.age = age;
this.address = address;
}
/**************重写toString()方法*************************/
@Override
public String toString() {
return "XXXObject [name=" + name + ", age=" + age + ", address="
+ address + "]";
}
public static void main(String[] args) {
XXXObject xxxObject = new XXXObject("zhangsan",15,"beijing");
System.out.println(xxxObject.toString());
}
}
打印结果:XXXObject [name=zhangsan, age=15, address=beijing]
[ 本帖最后由 杨志罡 于 2011-07-21 04:02 编辑 ] |