首页 > 其他分享 >idea调试时要注意的问题:idea中用于调试时,可能会隐式调用你写的toString方法,因为要看到调试封装的对象数据结果

idea调试时要注意的问题:idea中用于调试时,可能会隐式调用你写的toString方法,因为要看到调试封装的对象数据结果

时间:2022-09-29 13:55:48浏览次数:55  
标签:Engine 时要 String idea toString public 调试

问题引出:

问题探索:



问题解决:

源码参考:

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

相关文章