4 changed files with 40 additions and 0 deletions
@ -0,0 +1,8 @@ |
|||
package w5; |
|||
|
|||
class Circle extends Shape { |
|||
@Override |
|||
public void draw() { |
|||
System.out.println("Drawing Circle"); |
|||
} |
|||
} |
|||
@ -0,0 +1,19 @@ |
|||
package w5; |
|||
|
|||
public class Main { |
|||
public static void main(String[] args) { |
|||
// 创建图形对象
|
|||
Circle circle = new Circle(); |
|||
Rectangle rectangle = new Rectangle(); |
|||
|
|||
// 调用统一接口方法
|
|||
System.out.println("=== 开始绘图 ==="); |
|||
drawShape(circle); |
|||
drawShape(rectangle); |
|||
} |
|||
|
|||
public static void drawShape(Shape s) { |
|||
s.draw(); |
|||
} |
|||
} |
|||
|
|||
@ -0,0 +1,8 @@ |
|||
package w5; |
|||
|
|||
class Rectangle extends Shape { |
|||
@Override |
|||
public void draw() { |
|||
System.out.println("Drawing Rectangle"); |
|||
} |
|||
} |
|||
@ -0,0 +1,5 @@ |
|||
package w5; |
|||
|
|||
public abstract class Shape { |
|||
public abstract void draw(); |
|||
} |
|||
Loading…
Reference in new issue