java getSuperclass().toString()未运行我的自定义toString()
[嗨,伙计们。我正在通过Cay Horstmann的“为真正不耐烦的人准备的Java SE8”学习Java。学习第4章] 我想调用父类的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
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()
方法,它将始终从点打印声明的字段。即:在点中声明的字段
和在标签点中声明的字段