📄 shapesdemo.java
字号:
// 例2.7.3 ShapesDemo.java
interface Shape
{
void draw();
}
class Circle implements Shape
{
public void draw(){ System.out.println("Circle.draw()");}
}
class Square implements Shape
{
public void draw() { System.out.println("Square.draw()");}
}
class Triangle implements Shape
{
public void draw() { System.out.println("Triangle.draw()");}
}
public class ShapesDemo
{
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[5];
for ( int i=0; i<s.length; ++i )
s[i] = randShape(); // 随机生成5个图形类的对象
for ( int i=0; i<s.length; ++i )
s[i].draw(); // 晚绑定
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -