class Shape {
public void draw() {
}
}
class Cycle extends Shape {
@Override
public void draw() {
System.out.println("○");
}
}
class Rect extends Shape {
@Override
public void draw() {
System.out.println("□");
}
}
class Flower extends Shape {
@Override
public void draw() {
System.out.println("♣");
}
}
public class TestDemo1114_1 {
public static void main(String[] args) {
Shape shape1 = new Flower();
Shape shape2 = new Cycle();
Shape shape3 = new Rect();
drawShape(shape1);
drawShape(shape2);
drawShape(shape3);
}
// 打印单个图形
public static void drawShape(Shape shape) {
shape.draw();
}
}
1.父类应用引用子类对象 2.父类和子类都有同名的覆盖方法 3.通过父类引用这个重写方法的时候
1.类调用者对类的使用成本进—步降低 2.能够降低代码的"圈复杂度",避免使用大量的if - else 3.可扩展能力更强