作者PsMonkey (痞子军团团长)
看板java
标题Re: [问题] 请问toString的观念
时间Mon Apr 24 14:35:31 2006
※ 引述《seekyou (galagala~N )》之铭言:
: 如题,toString 会传回物件的字串表示法,意即将物件转成字串
: 下面有个小程式
: public class Employee {
: String name;
: int age;
: public static void main(String args[]) {
: Employee tom =new Employee();
: System.out.println(tom);
: System.out.println(tom.toString());
: }
: }
: 下面两行印出的结果都是一样的,不过为什麽toString()可加可不加呢?
: 可能对大家来说是理所当然的事 不过还是希望能听听各位的解说,非常谢谢
请看 System.out 的说明
然後找到 java.io.PrintStream
然後看一下 println(Object) 跟 println(String) 这两个 method。
也或许,你可以自己包一个 output 的 class
然後来交叉测试一下
public class PsOut{
public static void println(Object obj){
System.out.println("Sorry, PsMonkey can't handle this");
}
public static void println(String str){
System.out.println(str);
}
}
--
侃侃长论鲜窒碍 网站:
http://www.psmonkey.idv.tw
众目睽睽无心颤 个人版:telnet://legend.twbbs.org
茕居少聊常人事
杀头容易告白难 欢迎参观 Java 版(@ptt.cc) \囧/
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 203.204.16.17