You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

31 lines
708 B

// 1. 定义父类 Shape1
class Shape1 {
// 父类的 draw 方法
public void draw() {
System.out.println("绘制形状");
}
}
// 2. 定义子类 Circle 继承 Shape1 并重写 draw 方法
class Circle extends Shape1 {
@Override
public void draw() {
System.out.println("绘制圆形");
}
}
// 3. 定义子类 Rectangle 继承 Shape1 并重写 draw 方法
class Rectangle extends Shape1 {
@Override
public void draw() {
System.out.println("绘制矩形");
}
}
// 4. 定义工具类,包含 drawShape 方法
class ShapeDrawer {
// 接收 Shape1 类型参数,调用 draw 方法
public void drawShape(Shape1 s) {
s.draw();
}
}