You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
24 lines
578 B
24 lines
578 B
package w5基础版;
|
|
|
|
// 测试类
|
|
public class TestShape {
|
|
public static void main(String[] args) {
|
|
// 1. 创建对象
|
|
Shape circle = new Circle();
|
|
Shape rectangle = new Rectangle();
|
|
|
|
// 2. 调用统一绘制方法
|
|
drawShape(circle);
|
|
drawShape(rectangle);
|
|
|
|
// 3. 直接调用
|
|
System.out.println("\n=== 直接调用 ===");
|
|
circle.draw();
|
|
rectangle.draw();
|
|
}
|
|
|
|
// 核心方法:接收父类引用,实现多态调用
|
|
public static void drawShape(Shape s) {
|
|
s.draw();
|
|
}
|
|
}
|