题目: 有图形接口Shape,参照圆Circle类补充完整正方形Square和三角形Triangle类,并分析运行结果。
interface Shape {
void draw();
void erase();
}
class Circle implements Shape {
void draw() { System.out.println(“Circle.draw()”);}
void erase() { System.out.println(“Circle.erase()”);}
}
class Square implements Shape {
void draw() { }
void erase() { }
}
class Triangle implements Shape {
void draw() { }
void erase() { }
}
public class Shapes {
public static Shape randShape() {
switch((int)(Math.random() * 3)) {
default: // To quiet the compiler
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();
}
}
package JavaExperiment2;
interface Shape {
public void draw();
public void erase();
}
class Circle implements Shape {
public void draw(