pangyaxuan
|
92e49b2f14
|
上传文件至 'W5'
package w5;
// 基础题:Shape类及其子类
abstract class Shape {
public abstract void draw();
}
class Circle extends Shape {
private double r;
public Circle(double r) {
this.r = r;
}
@Override
public void draw() {
System.out.println("绘制圆形,半径:" + r);
}
}
class Rectangle extends Shape {
private double width;
private double height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
@Override
public void draw() {
System.out.println("绘制矩形,宽:" + width + ",高:" + height);
}
}
// 工具类
class ShapeUtil {
public static void drawShape(Shape s) {
s.draw();
}
}
// 主类
public class PolymorphismDemo {
public static void main(String[] args) {
System.out.println("=== 基础题测试 ===");
// 测试Shape类
Shape circle = new Circle(5);
Shape rectangle = new Rectangle(4, 6);
ShapeUtil.drawShape(circle);
ShapeUtil.drawShape(rectangle);
}
}
|
2 weeks ago |