commit
9db82eb845
1 changed files with 51 additions and 0 deletions
@ -0,0 +1,51 @@ |
|||||
|
public class ShapeDemo { |
||||
|
public static void main(String[] args) { |
||||
|
// 创建不同形状的对象
|
||||
|
Shape circle = new Circle(); |
||||
|
Shape rectangle = new Rectangle(); |
||||
|
|
||||
|
// 测试drawShape方法
|
||||
|
drawShape(circle); |
||||
|
drawShape(rectangle); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 调用形状的draw方法 |
||||
|
* @param s 形状对象 |
||||
|
*/ |
||||
|
public static void drawShape(Shape s) { |
||||
|
s.draw(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 形状基类 |
||||
|
*/ |
||||
|
class Shape { |
||||
|
/** |
||||
|
* 绘制方法 |
||||
|
*/ |
||||
|
public void draw() { |
||||
|
System.out.println("绘制形状"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 圆形类 |
||||
|
*/ |
||||
|
class Circle extends Shape { |
||||
|
@Override |
||||
|
public void draw() { |
||||
|
System.out.println("绘制圆形"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 矩形类 |
||||
|
*/ |
||||
|
class Rectangle extends Shape { |
||||
|
@Override |
||||
|
public void draw() { |
||||
|
System.out.println("绘制矩形"); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue