Object类toString()

Object类是java所有类的始祖,在java中每个类都是由它扩展而来。

toString()方法返回一个包含类名与内存地址的文本形式的字符串  即打印对象的时候便会调用此方法。

强烈建议为自定义的每一个类重写toString方法。

未重写:

public class Initialize {
private String name = "张飞";
private Integer age = 22;
public static void main(String[] args){
Initialize z = new Initialize();
System.out.println(z);
}
}

运行结果:

Object类toString()

重写后:

public class Initialize {
private String name = "张飞";
private Integer age = 22;
public static void main(String[] args){
Initialize z = new Initialize();
System.out.println(z);
} @Override
public String toString() {
return "Initialize{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}

运行结果:

Object类toString()

上一篇:memcached安装报错 error while loading shared libraries: libevent-2.0.so.5: cannot open shared object file: No such file or directory解决


下一篇:[HDU4669]Editor (栈)