1 changed files with 39 additions and 0 deletions
@ -0,0 +1,39 @@ |
|||||
|
// 父类 Shape |
||||
|
public class Shape { |
||||
|
public void draw() { |
||||
|
System.out.println("绘制一个图形"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// 子类 Circle |
||||
|
public class Circle extends Shape { |
||||
|
@Override |
||||
|
public void draw() { |
||||
|
System.out.println("绘制一个圆形 ○"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// 子类 Rectangle |
||||
|
public class Rectangle extends Shape { |
||||
|
@Override |
||||
|
public void draw() { |
||||
|
System.out.println("绘制一个矩形 ▭"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// 测试主类 |
||||
|
public class ShapeTest { |
||||
|
// 题目要求:drawShape 方法,接收 Shape 类型参数 |
||||
|
public static void drawShape(Shape s) { |
||||
|
s.draw(); // 多态调用 |
||||
|
} |
||||
|
|
||||
|
public static void main(String[] args) { |
||||
|
Shape circle = new Circle(); |
||||
|
Shape rect = new Rectangle(); |
||||
|
|
||||
|
System.out.println("=== 基础题测试 ==="); |
||||
|
drawShape(circle); |
||||
|
drawShape(rect); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue