5 changed files with 52 additions and 0 deletions
@ -0,0 +1,9 @@ |
|||
/** |
|||
* 矩形:重写draw()方法 |
|||
*/ |
|||
public class Rectangle extends Shape { |
|||
@Override |
|||
public void draw() { |
|||
System.out.println("绘制矩形 🟦"); |
|||
} |
|||
} |
|||
@ -0,0 +1,19 @@ |
|||
/** |
|||
* 测试类:包含drawShape()方法,统一调用draw() |
|||
*/ |
|||
public class TestShape { |
|||
// 统一绘制图形的方法 |
|||
public static void drawShape(Shape s) { |
|||
s.draw(); // 多态调用:执行子类重写的draw() |
|||
} |
|||
|
|||
public static void main(String[] args) { |
|||
// 创建不同图形对象 |
|||
Shape circle = new Circle(); |
|||
Shape rectangle = new Rectangle(); |
|||
|
|||
// 调用统一方法绘制 |
|||
drawShape(circle); |
|||
drawShape(rectangle); |
|||
} |
|||
} |
|||
@ -0,0 +1,6 @@ |
|||
/** |
|||
* 交通工具抽象类:定义抽象方法run() |
|||
*/ |
|||
public abstract class Vehicle { |
|||
public abstract void run(); |
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
/** |
|||
* 图形父类:定义draw()方法规范 |
|||
*/ |
|||
public class Shape { |
|||
// 绘制方法(父类默认实现) |
|||
public void draw() { |
|||
System.out.println("绘制基础图形"); |
|||
} |
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
/** |
|||
* 圆形:重写draw()方法 |
|||
*/ |
|||
public class Circle extends Shape { |
|||
@Override |
|||
public void draw() { |
|||
System.out.println("绘制圆形 ⭕"); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue