public String toString()的方法应该在什么时候使用
解决方案 »
- 初学者求JAVA方法名单词
- 关于 php ,java 取yahoo site ,yahoo link, google link ,google site
- JTable 可以对单个行的设定高度么?
- 网络通信,readUTF()异常
- 为什么我写的applet老是不能得到想要的显示
- .bat 文件的choice /?命令的问题
- 发几篇swing中关于JTable中的运用-1
- jbuilderX中发现一个Bug,大家看看怎样解决!
- 据说新键一个对话框来读取字符串 会增加一个线程 退出程序需要用System.exit() 可是这样别的程序也推出了 啊
- 一个关于线程的小问题
- 请教在将.class文件打包成.jar文件时出现的问题
- 一道阿里巴巴算法笔试题
但,转换的对象必须是非null的。
显式的就是在写代码的时候就手动地调用toString方法,将他转化为字符串。
隐式的就是在对象和字符串进行运算或者是将对象作为参数调用print、println方法的时候,会自动调用对象的toString方法。
比如:
public class TestString {
private void exchangeToString(Object str) {
String temp = "你也好:"+str;
System.out.println(temp);
}
public static void main(String[] args) {
StringExample stringExample = new StringExample(); System.out.println(stringExample);
System.out.println(stringExample.toString());
System.out.println("你好:" + stringExample);
System.out.println("你好:" + stringExample.toString());
TestString test=new TestString();
test.exchangeToString(stringExample);
}
}
class StringExample {
@Override
public String toString() {
// TODO Auto-generated method stub
return "hello";
}
}
输出为:
hello
hello
你好:hello
你好:hello
你也好:hello
大部分 System.out.printf(对象名);