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.
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 |
| .. |
|
NeteaseCommentCrawler.java
|
上传文件至 'W5'
|
2 weeks ago |
|
屏幕截图 2026-04-06 201413.png
|
上传文件至 'W5'
|
2 weeks ago |