1、System.out.println()里的参数会自动调用toString方法。
package com.mydemo.controller;
// 1.getClass().getName() + '@' + 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";
    }*/
}
View Code

相关文章:

  • 2021-11-23
  • 2021-05-30
  • 2021-12-31
  • 2021-09-11
  • 2021-11-04
  • 2021-05-30
  • 2021-09-12
  • 2022-12-23
猜你喜欢
  • 2021-11-01
  • 2022-12-23
  • 2021-06-01
  • 2021-10-01
  • 2022-01-01
  • 2021-09-23
  • 2021-08-25
相关资源
相似解决方案