- toString
-
返回对象的字符串表示形式。 通常,public String toString()
toString
方法返回一个“文本表示”此对象的字符串。 结果应该是简洁但信息丰富的表示,便于人们阅读。 建议所有子类都覆盖此方法。类
Object
的toString
方法返回一个字符串,该字符串由对象为实例的类的名称,符号字符“@
”以及对象的哈希码的无符号十六进制表示形式组成。 换句话说,此方法返回一个等于值的字符串:getClass().getName() + '@' + Integer.toHexString(hashCode())
结果
对象的字符串表示形式。
-
-
public class qxy { public static void main(String[] args) { person p=new person("ZHANGSAN",18); System.out.println(p); System.out.println(p.toString()); } } public class person { private String name; private int age; 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 person(String name, int age) { super(); this.name = name; this.age = age; } public person() { super(); // TODO Auto-generated constructor stub } } lzh.person@6d06d69c lzh.person@6d06d69c
public class qxy { public static void main(String[] args) { person p=new person("ZHANGSAN",18); System.out.println(p); System.out.println(p.toString()); } } public class person { private String name; private int age; 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 person(String name, int age) { super(); this.name = name; this.age = age; } public person() { super(); // TODO Auto-generated constructor stub } public String toString() { return "我是这样一个人"+this.name+this.age; } } 重写toString 我是这样一个人ZHANGSAN18 我是这样一个人ZHANGSAN18
toString
最新推荐文章于 2022-05-03 11:27:01 发布