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.
38 lines
938 B
38 lines
938 B
public class Shape1 {
|
|
public void draw() {
|
|
System.out.println("绘制一个图形");
|
|
}
|
|
|
|
// 静态方法,用于测试多态
|
|
public static void drawShape(Shape s) {
|
|
s.draw();
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
System.out.println("--- 基础题测试 ---");
|
|
|
|
// 创建子类对象
|
|
Circle c = new Circle();
|
|
Rectangle r = new Rectangle();
|
|
|
|
// 调用测试方法,体现多态性
|
|
drawShape(c); // 输出: 绘制一个圆形
|
|
drawShape(r); // 输出: 绘制一个矩形
|
|
}
|
|
}
|
|
|
|
// 子类 Circle1,注意这里不要加 public
|
|
class Circle1 extends Shape {
|
|
@Override
|
|
public void draw() {
|
|
System.out.println("绘制一个圆形");
|
|
}
|
|
}
|
|
|
|
// 子类 Rectangle1,注意这里不要加 public
|
|
class Rectangle1 extends Shape {
|
|
@Override
|
|
public void draw() {
|
|
System.out.println("绘制一个矩形");
|
|
}
|
|
}
|
|
|