ch9ex11.java

来自「JAVA程序设计 丁岳伟 彭敦陆编 高等教育出版社 第7---11章程序」· Java 代码 · 共 46 行

JAVA
46
字号
class Shape {
	void draw(){}
	void erase(){} 
}
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 ch9ex11{
  	public static Shape randShape(){
    	switch((int)(Math.random()*3)){
      		case 0: return new Circle();
      		case 1: return new Square();
      		case 2: return new Triangle();
      		default:return null;
    	}
  	}
  	public static void main(String[] args){
    	Shape s[]=new Shape[9];
    	for(int j=0;j<s.length;j++)
      		s[j]=randShape();
    	for(int j=0;j<s.length;j++)
      		s[j].draw();
  	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?