class Circle extends Shape{ void draw(){ System.out.println("circle.draw()"); } void erase(){ System.out.println("circle.erase()"); } }
class Square extends Shape{ void draw(){ System.out.println("square.draw()"); } void erase(){ System.out.println("square.erase()"); } } class Triangle extends Shape{ void draw(){ System.out.println("triangle.draw()"); } void erase(){ System.out.println("triangle.erase()"); } } public class DongTaiBangDing {
public static Shape randShape(){ //此处 提示 this method must return a type of Shape,该怎么改呢???(知道这是 要 返回 一个Shape 类型 变量的意思;) switch((int)(Math.random()*3)){ case 0: return new Circle(); case 1: return new Square(); case 2: return new Triangle(); } } public static void main(String args[]){ Shape[] s=new Shape[9]; for(int i=0;i<s.length;i++){ s[i]=randShape(); } for(int j=0;j<s.length;j++){ s[j].draw(); } }