package 继承;public class Example5_18{public static void main(String args[]) {//创建三个Shape对象Shape shape[] = new Shape[3];shape[0] = new Circle(50, 50, 40);shape[1] = new Rectangle(0, 0, 40, 30);shape[2] = new RectangleEx(20, 30, 40, 30, 5);//创建三个Shape对象的信息数组String ShapeName[] = new String[3];ShapeName[0] = "圆Circle(50, 50, 40)";ShapeName[1] = "矩形Rectangle(0, 0, 40, 30)";ShapeName[2] = "圆角矩形RectangleEx(20, 30, 40, 30, 5)";//求面积和周长for(int i = 0; i < shape.length; i++) {System.out.println(ShapeName[i] + "的面积=" + shape[i].area());System.out.println(ShapeName[i] + "的周长=" + shape[i].perimeter());System.out.println();}}}class Shape{public final double PI = 3.141592654;double area(){ return 0; };double perimeter(){ return 0; };}//子类Circle的声明class Circle extends Shape{int centerX; //圆心X坐标int centerY; //圆心Y坐标int radius; //圆的半径public Circle(int x, int y, int r) {super();centerX = x;centerY = y;radius = r;}public double area() {return (int)(PI * radius * radius );}public double perimeter() {return (int)(2 * PI * radius);}}class Rectangle extends Shape{int left; //矩形左上角X坐标int top; //矩形左上角Y坐标int width; //矩形长度int height; //矩形宽度public Rectangle(int l, int t, int w, int h) {super();left = l;top = t;width = w;height = h;}public double area() {return (int)(width * height);}public double perimeter() {return (int)((width + height) * 2);}}class RectangleEx extends Rectangle{int radius; //圆角半径public RectangleEx(int l, int t, int w, int h, int r){super(l, t, w, h);radius = r;}public double area() {return (int)(super.area() - (4 - PI) * radius* radius);}public double perimeter() {return (int)((width + height)*2 - 8*radius + 2*PI*radius);}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。