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.
17 lines
660 B
17 lines
660 B
public class ShapeTest {
|
|
// 多态方法:接收Shape类型参数,调用对应子类的draw
|
|
public static void drawShape(Shape s) {
|
|
s.draw();
|
|
}
|
|
public static void main(String[] args) {
|
|
// 1. 创建不同子类对象
|
|
Shape circle = new Circle();
|
|
Shape rectangle = new Rectangle();
|
|
Shape shape = new Shape();
|
|
// 2. 调用多态方法测试
|
|
System.out.println("=== 基础题多态测试 ===");
|
|
drawShape(shape); // 调用父类Shape的draw
|
|
drawShape(circle); // 调用Circle的draw
|
|
drawShape(rectangle); // 调用Rectangle的draw
|
|
}
|
|
}
|