複寫toString這個方法,IDE一般都有自動生成
代碼如下,建構子我用lombok可以無視
import lombok.AllArgsConstructor;
public class TestObj {
public static void main(String[] args) {
TextDB t1 = new TextDB("test1",true);
System.out.println("沒複寫toString:"+t1);
//沒複寫toString:TestObj$TextDB@77afea7d
TextDBToString t2 = new TextDBToString("test2",true);
System.out.println("複寫toString:"+t2);
//複寫toString:TextDBToString{s1='test2', f1=true}
}
@AllArgsConstructor
private static class TextDB {
private String s1;
private Boolean f1;
}
@AllArgsConstructor
private static class TextDBToString {
private String s1;
private Boolean f1;
@Override
public String toString() {
return "TextDBToString{" +
"s1='" + s1 + '\'' +
", f1=" + f1 +
'}';
}
}
}