testshape.java

来自「我在学习JAVA的讲义」· Java 代码 · 共 39 行

JAVA
39
字号
abstract class Shape {
	abstract double area();
}

class Circle extends Shape {
	public int r;

	Circle(int r) {
		this.r = r;
	}

	public double area() {
		return 3.14 * r * r;
	}
}

class Rectangle extends Shape {
	public int width, height;

	Rectangle(int w, int h) {
		width = w;
		height = h;
	}

	public double area() {
		return width * height;
	}
}

class Test {
	public static void main(String[] args) {
		double area_total = 0.0;
		Shape[] shape = { new Circle(2), new Rectangle(5, 2), new Circle(3) };
		for (int i = 0; i < shape.length; i++) {
			area_total += shape[i].area();
		}
		System.out.println("Area = " + area_total);
	}
}

⌨️ 快捷键说明

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