useshapes.java
来自「用java画圆 用这个程序可以实现自动画图。」· Java 代码 · 共 35 行
JAVA
35 行
/*
* Created on 2005-10-9
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package polymorphism;
/**
* From 'Thinking in Java, 2nd ed.' by Bruce Eckel
*/
public class UseShapes {
public static Shape randShape() {
switch ((int) (Math.random() * 3)) {
default:
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];
// Fill up the array with shapes:
for (int i = 0; i < s.length; i++)
s[i] = randShape();
// Make polymorphic method calls:
for (int i = 0; i < s.length; i++)
s[i].draw();
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?