我有一个具有 toString() 方法的类形状。我有另一个扩展形状的类圆。这个圆类覆盖了继承的 toString() 方法。我想要做的就是调用父类-形状的从toString()方法中的圆的toString()方法。以下是我目前所做的。我认为在圆的 toString() 中调用 toString() 可能会调用继承的 toString() 但这只会进入无限循环。形状类:class shape{String color;boolean filled;shape(){ color = "green"; filled = true;}shape(String color, boolean fill){ this.color = color; this.filled = fill;}public String toString(){ String str = "A Shape with color = " + color + " and filled = " + filled + " ."; return str;}}圈类:class circle extends shape{double radius;circle(){ this.radius = 1.0;}public String toString(){ String str = "A circle with radius " + radius + " and which is a subclass of " + toString(); return str;}
添加回答
举报
0/150
提交
取消