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.
51 lines
885 B
51 lines
885 B
public class ShapeDemo {
|
|
public static void main(String[] args) {
|
|
// 创建不同形状的对象
|
|
Shape circle = new Circle();
|
|
Shape rectangle = new Rectangle();
|
|
|
|
// 测试drawShape方法
|
|
drawShape(circle);
|
|
drawShape(rectangle);
|
|
}
|
|
|
|
/**
|
|
* 调用形状的draw方法
|
|
* @param s 形状对象
|
|
*/
|
|
public static void drawShape(Shape s) {
|
|
s.draw();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 形状基类
|
|
*/
|
|
class Shape {
|
|
/**
|
|
* 绘制方法
|
|
*/
|
|
public void draw() {
|
|
System.out.println("绘制形状");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 圆形类
|
|
*/
|
|
class Circle extends Shape {
|
|
@Override
|
|
public void draw() {
|
|
System.out.println("绘制圆形");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 矩形类
|
|
*/
|
|
class Rectangle extends Shape {
|
|
@Override
|
|
public void draw() {
|
|
System.out.println("绘制矩形");
|
|
}
|
|
}
|