首页 > 代码库 > java===tostring

java===tostring

package cn.china;
/**toString()方法用于打印对象信息;一般情况下需要重写此方法,以便于阅读;*/
class person{
    private int age;
    private String name;
    public person(int age,String name){
        this.age=age;
        this.name=name;
    }
    public void tell(){
        System.out.println("tell...run");
    }
    public String toString (){
        return "person 姓名:"+name+"\t"+"年龄:"+age;
    }
}
public class ToStringDemo {
    public static void main(String[]a){
        person p = new person(20,"tom");
        System.out.println(p);
    }

}

 

java===tostring