Java 对象的 toString() 方法 -Java 学习笔记 (16)
2007-05-31 23:44
591 查看
Java中所有对象都自Object类继承了toString()方法。
toString()方法返回当前对象的字符串表示
在用到对象的字符串表示时,系统会自动调用对象的toString()方法,如print()函数,字符串"+"运算等。
自定义类的toString()表示
public class ToStringTest ...{
public static void main (String[] args) ...{
Person p1 = new Person("zhaohongliang");
System.out.println(p1);
}
}
class Person ...{
private String name;
public Person(String name) ...{
this.name=name;
}
}
运行System.out.println(p1);时
系统调用的是p1的toString()方法
运行结果:
Person@1fc4bec
这个结果看起来怪怪的,这是因为在默认情况下(不重载),toString()的结果格式为
类名@对象的16进制Hash表示
Object类toString()方法的API Docs
Returns a string representation of the object. In general, the
The
Returns: a string representation of the object.
所以,如果可能用到的话,应当重写这个方法
public class ToStringTest ...{
public static void main (String[] args) ...{
Person p1 = new Person("zhaohongliang");
System.out.println(p1);
}
}
class Person ...{
private String name;
public Person(String name) ...{
this.name=name;
}
public String toString() ...{
return "My name is "+this.name+".";
}
}
运行结果
My name is zhaohongliang.
toString()方法返回当前对象的字符串表示
在用到对象的字符串表示时,系统会自动调用对象的toString()方法,如print()函数,字符串"+"运算等。
自定义类的toString()表示
public class ToStringTest ...{
public static void main (String[] args) ...{
Person p1 = new Person("zhaohongliang");
System.out.println(p1);
}
}
class Person ...{
private String name;
public Person(String name) ...{
this.name=name;
}
}
运行System.out.println(p1);时
系统调用的是p1的toString()方法
运行结果:
Person@1fc4bec
这个结果看起来怪怪的,这是因为在默认情况下(不重载),toString()的结果格式为
类名@对象的16进制Hash表示
Object类toString()方法的API Docs
toString
public String toString()
Returns a string representation of the object. In general, the
toStringmethod returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.
The
toStringmethod for class
Objectreturns a string consisting of the name of the class of which the object is an instance, the at-sign character `
@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:
getClass().getName() + '@' + Integer.toHexString(hashCode())
Returns: a string representation of the object.
所以,如果可能用到的话,应当重写这个方法
public class ToStringTest ...{
public static void main (String[] args) ...{
Person p1 = new Person("zhaohongliang");
System.out.println(p1);
}
}
class Person ...{
private String name;
public Person(String name) ...{
this.name=name;
}
public String toString() ...{
return "My name is "+this.name+".";
}
}
运行结果
My name is zhaohongliang.
相关文章推荐
- Java学习笔记43. 使用对象作为方法的参数
- JNI学习笔记:(1)开篇(2)本地代码访问Java代码 (3)本地方法取得Java属性/调用java方法 (4)本地代码创建Java对象(包括javaString) (5) 本地方法处理java数组
- Java菜鸟学习笔记--面向对象篇(十六):Object类方法
- Java学习笔记16(面向对象九:补充内容)
- Java菜鸟学习笔记--面向对象篇(十六):Object类方法
- java学习笔记《java面向对象编程》——面向对象开发方法概述
- java学习笔记(四)----对象、数组作为参数传递,静态变量、静态方法的使用,内部类,使用文档注释
- 【Java学习笔记】父类引用指向子类对象时方法的调用
- Java学习之面向对象(1.Object类,(1)toString方法,(2)equals方法,2.对象转型(casting),3.动态绑定与多态,4.抽象类(abstract),5.Final关键字)
- java学习笔记-JAVA中toString方法的作用
- 多态Object类toString方法学习笔记ObjectDemo.java
- Java学习笔记12(面向对象五:构造方法、this再探)
- Java学习笔记(16)--方法重载篇
- java 学习笔记3-类、对象、方法
- 【Java学习笔记】String对象的不变性和intern()方法——基于Java SE8
- 【学习笔记16】java面向对象-异常
- <Head First Java>学习笔记--第四章:方法操作实例变量 对象的行为
- JNI学习笔记(四)JNI中本地语言创建Java对象并且访问具体方法(附例子)
- 常见对象String和其方法+JAVA学习笔记-DAY12
- Java学习笔记之toString()方法学习。