基本介绍
默认返回:全类名+@+哈希值的16进制,子类往往重写toString方法,用于返回对象的属性信息重写toString方法,打印对象或拼接对象时,都会自动调用该对象的toString形式
当直接输出一个对象时,toString方法会被默认的调用
package com.java.Object_;
public class toString_ {
public static void main(String[] args) {
Monster monster = new Monster("jack", "fanshan", 10000);
System.out.println(monster.toString());
System.out.println(monster);
}
}
class Monster{
private String name;
private String job;
private double sar;
public Monster(String name, String job, double sar) {
this.name = name;
this.job = job;
this.sar = sar;
}
//重写
@Override
public String toString() {
return "Monster{" +
"name='" + name + '\'' +
", job='" + job + '\'' +
", sar=" + sar +
'}';
}
}