shapes.java

来自「翁剀JAVA语言那门课程的教案 很多人都看多他的视频教程可惜没有ppt的教案」· Java 代码 · 共 54 行

JAVA
54
字号
//: Shapes.java
// Polymorphism in Java

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 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 polymorphosic method calls
		for ( int i=0; i<s.length; ++i )
			s[i].draw();
	}
}

⌨️ 快捷键说明

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