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