2 changed files with 47 additions and 0 deletions
@ -0,0 +1,38 @@ |
|||
public class Shape { |
|||
public void draw() { |
|||
System.out.println("绘制一个图形"); |
|||
} |
|||
|
|||
// 静态方法,用于测试多态
|
|||
public static void drawShape(Shape s) { |
|||
s.draw(); |
|||
} |
|||
|
|||
public static void main(String[] args) { |
|||
System.out.println("--- 基础题测试 ---"); |
|||
|
|||
// 创建子类对象
|
|||
Circle c = new Circle(); |
|||
Rectangle r = new Rectangle(); |
|||
|
|||
// 调用测试方法,体现多态性
|
|||
drawShape(c); // 输出: 绘制一个圆形
|
|||
drawShape(r); // 输出: 绘制一个矩形
|
|||
} |
|||
} |
|||
|
|||
// 子类 Circle1,注意这里不要加 public
|
|||
class Circle extends Shape { |
|||
@Override |
|||
public void draw() { |
|||
System.out.println("绘制一个圆形"); |
|||
} |
|||
} |
|||
|
|||
// 子类 Rectangle1,注意这里不要加 public
|
|||
class Rectangle extends Shape { |
|||
@Override |
|||
public void draw() { |
|||
System.out.println("绘制一个矩形"); |
|||
} |
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
1 |
|||
22 |
|||
33 |
|||
66 |
|||
55 |
|||
692 |
|||
11 |
|||
335 |
|||
555 |
|||
Loading…
Reference in new issue