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.
36 lines
688 B
36 lines
688 B
// 父类Shape
|
|
class Shape {
|
|
public void draw() {
|
|
// 默认实现
|
|
}
|
|
}
|
|
|
|
// 子类Circle
|
|
class Circle extends Shape {
|
|
public void draw() {
|
|
System.out.println("Drawing a circle");
|
|
}
|
|
}
|
|
|
|
// 子类Rectangle
|
|
class Rectangle extends Shape {
|
|
public void draw() {
|
|
System.out.println("Drawing a rectangle");
|
|
}
|
|
}
|
|
|
|
// 测试类
|
|
class ShapeTest {
|
|
// drawShape方法
|
|
public static void drawShape(Shape shape) {
|
|
shape.draw();
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
Circle circle = new Circle();
|
|
Rectangle rectangle = new Rectangle();
|
|
|
|
drawShape(circle);
|
|
drawShape(rectangle);
|
|
}
|
|
}
|