Java 中重写 toString()

package cn.onexzb.www;

public class TestOverride {
    String name;
    int age;
    String add;
    
    TestOverride(String name, int age, String add){
        this.name = name;
        this.age = age;
        this.add = add;
    }
    
    public String toString()
    {
        return "name:"+ name +"  "+"age:" + age + "     " + "add:" + add;
    }
    
    public static void main(String[] args) {
        TestOverride temp = new TestOverride("Bob", 18, "Anhui");
        String result = temp.toString();
        System.out.println(result);
    }
}
最后修改:2019 年 04 月 27 日 04 : 30 PM

发表评论