本帖最后由 赵海洋 于 2013-3-1 14:56 编辑
首先是一段很简答的代码,在论坛里看到的:- public class TestString
- {
- private String name;
- private int age;
- public TestString(){}
- public TestString(String name,int age)
- {
- this.name = name;
- this.age = age;
- }
- public String toString()
- {
- return name+age;
- }
- public static void main(String[] args)
- {
- TestString test = new TestString("lisa",23);
- System.out.println(test);
- }
- }
复制代码 然后呢,打印出来是lisa23,这端代码打印的是test,而这个lisa23其实应该是打印的test.toString(),于是我这么做了:- public class TestString
- {
- private String name;
- private int age;
- public TestString(){}
- public TestString(String name,int age)
- {
- this.name = name;
- this.age = age;
- }
- public static void main(String[] args)
- {
- TestString test = new TestString("lisa",23);
- System.out.println(test);
- }
- }
复制代码 我把重写的toString语句删掉,再打印是TestString@145c859,也就是打印的哈希值,是不是可以理解为:如果我这里直接打印对象test,默认执行的其实是test.toString()。
求解释,求解释~~到底对不对????
|