问题引出:
问题探索:
问题解决:
源码参考:
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()); //
}
}
标签:Engine,时要,String,idea,toString,public,调试
From: https://www.cnblogs.com/rong-xu-drum/p/16741248.html