public class test { private String name = "123"; private int id = 1; private boolean married = false; @Override public String toString() { return "test{" + "name='" + name + '\'' + ", id=" + id + ", married=" + married + '}'; } public static void main(String[] args) { test t =new test(); System.out.println(t); String s= "This is a String:" + t; System.out.println(s); } }
我们看到他给我们输出了一个@c,我也不知道具体是什么,也许是他的标号或存储地址,总之是Object里toString方法给我们返回的String,有兴趣的同学可以去查看Object的源码。
在这里提醒一下各位,toString方法可以手动重写,也可以利用编译器直接重写哦(如果你没有特殊需求,有编译器帮我们写何乐而不为呢?)下面介绍在idea里面如何要编译器帮我们重写。

3、

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/211779.html原文链接:https://javaforall.net
