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.
34 lines
806 B
34 lines
806 B
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("画一个矩形");
|
|
}
|
|
}
|
|
|
|
public class ShapeTest{
|
|
public static void main(String[]args){
|
|
Shape s1 = new Circle();
|
|
Shape s2 = new Rectangle();
|
|
|
|
s1.draw();
|
|
s2.draw();
|
|
|
|
Shape[]shapes={new Circle(),new Rectangle(),new Circle()};//两个new Circle 是为了演示数组中可以有多个相同类型对象共存
|
|
for(Shape s:shapes){
|
|
s.draw();//每个对象调用自己的draw版本
|
|
}
|
|
}
|
|
}
|