java getSuperclass()。toString()没有运行我的自定义toString()

时间:2017-07-22 16:04:00

标签: java reflection tostring superclass

[嗨,伙计们。我正在使用Cay Horstmann的“Java SE8 for the Itally Impatient”学习Java。学习第4章]

我想调用父类的toString()方法并向其添加内容。不幸的是,调用父类的toString()方法似乎不起作用。

到目前为止,我已经有了这些课程:

public class Point {
    private double _x = 0;
    private double _y = 0;

    public Point(double x, double y) {
        _x=x;
        _y=y;
    }

    @Override
    public String toString() {

        String theString = getClass().getSuperclass().toString() + " => " + getClass().getCanonicalName();

        theString += "[";
        int i=0;
        for (Field theField : getClass().getDeclaredFields()) {
            theField.setAccessible(true);
            try {
                if (i>0) theString += ", ";
                theString += theField.getName() + ": " + theField.get(this).toString();
                i++;
            } catch (IllegalArgumentException ex) {
                Logger.getLogger(Point.class.getName()).log(Level.SEVERE, null, ex);
                System.out.println("err1");
            } catch (IllegalAccessException ex) {
                Logger.getLogger(Point.class.getName()).log(Level.SEVERE, null, ex);
                System.out.println("err2");
            }
        }
        theString += "]";
        return theString;
    }
    ...        
}

public class LabeledPoint extends Point {
    private String _label = "";

    public LabeledPoint(String label, double x, double y) {
        super(x, y);
        this._label = label;
    }

    ...

}

我用这个main方法打电话给他们:

public static void main (String[] args) {
    LabeledPoint lp1 = new LabeledPoint("FirstPoint", 10.5, 30);
    System.out.println("The new point is:");
    System.out.println(lp1);
}

所以,我期待得到这样的东西:

Point[_x: 10.5, _y:30.0]=>LabeledPoint[_label: FirstPoint]

但相反,我

Point=>LabeledPoint[_label: FirstPoint]

也就是说,getClass().getSuperclass().toString() 执行Point.toString(),但它只是打印出规范名称。

为什么?

2 个答案:

答案 0 :(得分:1)

您似乎需要super.toString()而不是getClass().getSuperClass().toString()

答案 1 :(得分:0)

好的,伙计们。 (我不知道这有多好,但它有效)

Point类看起来像这样:

public class Point {
    private double _x = 0;
    private double _y = 0;

    public Point(double x, double y) {
        _x=x;
        _y=y;
    }

    @Override
    public String toString() {

        String theString =  super.toString() + "=>" + getClass().getCanonicalName();

        //The following creates an array of fields that includes the parent 'Point's fields if the object is an instance of a direct child class of Point
        Field[] theFields;
        if (this.getClass() == Point.class) {
            theFields = getClass().getDeclaredFields();
        } else {

            theFields = new Field[Point.class.getDeclaredFields().length+getClass().getDeclaredFields().length];
            System.arraycopy(
                    Point.class.getDeclaredFields(), 
                    0, 
                    theFields, 
                    0, 
                    Point.class.getDeclaredFields().length
            );
            System.arraycopy(
                    getClass().getDeclaredFields(), 
                    0, 
                    theFields, 
                    Point.class.getDeclaredFields().length, 
                    getClass().getDeclaredFields().length
            );


        }

        theString += "[";
        int i=0;
        for (Field theField : theFields) {
            theField.setAccessible(true);
            try {
                if (i>0) theString += ", ";
                theString += theField.getName() + ": " + theField.get(this).toString();
                i++;
            } catch (IllegalArgumentException ex) {
                Logger.getLogger(Point.class.getName()).log(Level.SEVERE, null, ex);
                System.out.println("err1");
            } catch (IllegalAccessException ex) {
                Logger.getLogger(Point.class.getName()).log(Level.SEVERE, null, ex);
                System.out.println("err2");
            }
        }

        theString += "]";

        return theString;
    }
    ...
}