shapes.java

来自「java入门及程序 介绍interface 实现 使用」· Java 代码 · 共 45 行

JAVA
45
字号
interface Shape { 
  void draw() ;
  void erase() ; 
}
class Circle implements Shape {
  public void draw() { 
    System.out.println("Circle.draw()"); 
  }
  public void erase() { 
    System.out.println("Circle.erase()"); 
  }
}
class Square implements Shape {
  public void draw() { 
    System.out.println("Square.draw()"); 
  }
  public void erase() { 
    System.out.println("Square.erase()"); 
  }
}
class Triangle implements Shape {
  public void draw() { 
    System.out.println("Triangle.draw()"); 
  }
  public void erase() { 
    System.out.println("Triangle.erase()");
  }
}
public class Shapes {
  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];
    for(int i = 0; i < s.length; i++)
      s[i] = randShape();
    for(int i = 0; i < s.length; i++)
      s[i].draw();
  }
}

⌨️ 快捷键说明

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