Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java getSuperclass().toString()未运行我的自定义toString()_Java_Reflection_Tostring_Superclass - Fatal编程技术网

java getSuperclass().toString()未运行我的自定义toString()

java getSuperclass().toString()未运行我的自定义toString(),java,reflection,tostring,superclass,Java,Reflection,Tostring,Superclass,[嗨,伙计们。我正在通过Cay Horstmann的“为真正不耐烦的人准备的Java SE8”学习Java。学习第4章] 我想调用父类的toString()方法并向其中添加一些内容。不幸的是,对父类的toString()方法的调用似乎不起作用 到目前为止,我已经完成了以下课程: public class Point { private double _x = 0; private double _y = 0; public Point(double x, double

[嗨,伙计们。我正在通过Cay Horstmann的“为真正不耐烦的人准备的Java SE8”学习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]
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;
    }
    ...
}
但是相反,我变得越来越坚强:

Point=>LabeledPoint[_label: FirstPoint]
也就是说,
getClass().getSuperclass().toString()
不是执行
Point.toString()
,而是打印出规范名称


为什么呢?

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

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

类如下所示:

Point[_x: 10.5, _y:30.0]=>LabeledPoint[_label: FirstPoint]
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;
    }
    ...
}

您正在
类上调用
toString()
(不是实例)。太好了!那么,如何调用此实例的父级
toString()
方法呢?我尝试了
super.toString()
,但得到了
Object.toString()
方法,即:
LabeledPoint@7852e922=>…
您尚未在代码中显示您已在LabeledPoint中重写的字符串。因此,当您在lp1上调用toString时,实际上是在调用其父类Point的toString。谢谢,Codebender,但我尝试了
super.toString()
,但我得到了
Object.toString()
方法,即:
LabeledPoint@7852e922=>…
@JoseVelas这是因为它在
toString()中
点的方法
,因此超类是
对象
。你的意思是把代码放在标签点上吗?@bcsb1001:如果我把代码放在标签点上,那么我得到的结果是一样的:
LabeledPoint@7852e922=>标签点[\u标签:第一点]
。我想做的是使用“反射”,不必关心哪个子类调用
toString()
方法,它将始终从
点打印声明的字段。即:在
点中声明的字段
和在
标签点中声明的字段