idea调试时要注意的问题:idea中用于调试时,可能会隐式调用你写的toString方法,因为要看到调试封装的对象数据结果
问题引出:
问题探索:
问题解决:
源码参考:
package com.xu1.reusing.exercise;
/**
* @author xu
* @Description
* @Modified By:
*/
class Engine {
private String s;
Engine() {
System.out.println("Engine()");
s = "Constructed";
}
@Override
public String toString() { return s; }
}
public class Exercise1AirPlane {
private String fuselage, wings, tail;
private Engine e;
private Engine e2;
public Exercise1AirPlane() {
System.out.println("Inside Airplane()");
fuselage = "Body";
wings = "Airfoils";
tail = "Empennage";
}
@Override
public String toString() {
System.out.println("NEW: the class toString method of ExerciseAirPlane is working");
// lazy (delayed) initialization:
if(e == null) {
e = new Engine();
}
return "fuselage = " + fuselage + ", " +
"wings = " + wings + ", " +
"tail = " + tail + ", " +
"Engine = " + e;
}
public static void main(String[] args) {
// 调试还要执行 额外的代码,里面的某句代码(现在告诉你是那句代码)会无意调用到隐式的toString,
Exercise1AirPlane N1234 = new Exercise1AirPlane();
// System.out.println(N1234.toString()); //
}
}