- package com.mydemo.controller;
- //1.getClass().getName() +[email protected]+ Integer.toHexString(hashCode())
- public class TestToString {
- public static void main(String[] args) {
- Dog dog =new Dog();
- // 3.下面这三个方法都调用了dog 类的toString方法
- System.out.println(dog);
- System.out.println("dog:" + dog);
- System.out.println("dog:" + dog.toString());
- }
- }
- // 2.这个类继承了Object 类
- class Dog {
- // 还可以重写Object的toString方法
- /*public String toString() {
- return "I am cool dog";
- }*/
- }
来源: http://www.bubuko.com/infodetail-2133451.html