Rectangle和Ellipse这两个类都需要覆盖这两个抽象方法。
要变通解决此问题,您有3个选项:
- 两种方法加法
- 使每个扩展Shape的类抽象
- 有一个方法可以执行将扩展Shape的类的功能,并在Rectangle和Ellipse中覆盖该方法,例如:
abstract class Shape { // ... void draw(Graphics g); }和
class Rectangle extends Shape { void draw(Graphics g) { // ... } }最后
class Ellipse extends Shape { void draw(Graphics g) { // ... } }您可以在它们之间切换,如下所示:
Shape shape = new Ellipse(); shape.draw(); shape = new Rectangle(); shape.draw();
再次,只是一个例子。



