我有以下两个类,如下所示。为了简单起见,仅显示toString重写的方法。
public class Circle {
@Override
public String toString() {
return "Circle";
}
}
public class Cylinder extends Circle {
@Override
public String toString() {
// return "Cylinder"+this; // runs without explicitly calling toString() on this keyword
// return "Cylinder"+super; // throws error, asks to delete super token
return "Cylinder["+super.toString(); // runs without error after adding .toString() with super keyword
}
}
我遇到的问题是我对超级关键字的理解。super关键字的toString()应该像该关键字一样被隐式调用。实际上,大多数教程和书籍都将super关键字称为对超类的对象引用,因此它的行为必须与与toString()中的“ +”串联运算符一起使用时,此关键字。请帮助我理解这一点。
互换的青春
UYOU
相关分类