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.
39 lines
865 B
39 lines
865 B
// 父类 Shape
|
|
public class Shape {
|
|
public void draw() {
|
|
System.out.println("绘制一个图形");
|
|
}
|
|
}
|
|
|
|
// 子类 Circle
|
|
public class Circle extends Shape {
|
|
@Override
|
|
public void draw() {
|
|
System.out.println("绘制一个圆形 ○");
|
|
}
|
|
}
|
|
|
|
// 子类 Rectangle
|
|
public class Rectangle extends Shape {
|
|
@Override
|
|
public void draw() {
|
|
System.out.println("绘制一个矩形 ▭");
|
|
}
|
|
}
|
|
|
|
// 测试主类
|
|
public class ShapeTest {
|
|
// 题目要求:drawShape 方法,接收 Shape 类型参数
|
|
public static void drawShape(Shape s) {
|
|
s.draw(); // 多态调用
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
Shape circle = new Circle();
|
|
Shape rect = new Rectangle();
|
|
|
|
System.out.println("=== 基础题测试 ===");
|
|
drawShape(circle);
|
|
drawShape(rect);
|
|
}
|
|
}
|