题目描述:
前言
前面题目形状中我们看到,为了输出所有形状的周长与面积,需要建立多个数组进行多次循环。这次试验使用继承与多态来改进我们的设计。
本题描述
1.定义抽象类Shape
**属性:**不可变静态常量double PI
,值为3.14
,
抽象方法:public double getPerimeter()
,public double getArea()
2.Rectangle与Circle类均继承自Shape类。
Rectangle类(**属性:**int width,length)、Circle类(**属性:**int radius)。
带参构造方法为Rectangle(int width,int length)
,Circle(int radius)
。
toString
方法(Eclipse自动生成)
3.编写double sumAllArea
方法计算并返回传入的形状数组中所有对象的面积和与
double sumAllPerimeter
方法计算并返回传入的形状数组中所有对象的周长和。
4.main方法
4.1 输入整型值n,然后建立n个不同的形状。如果输入rect,则依次输入宽、长。如果输入cir,则输入半径。
4.2 然后输出所有的形状的周长之和,面积之和。并将所有的形状信息以样例的格式输出。 **提示:**使用Arrays.toString
。
4.3 最后输出每个形状的类型与父类型.使用类似shape.getClass()
//获得类型, shape.getClass().getSuperclass()
//获得父类型;
**注意:**处理输入的时候使用混合使用nextInt
与nextLine
需注意行尾回车换行问题。
思考
-
你觉得sumAllArea和sumAllPerimeter方法放在哪个类中更合适?
-
是否应该声明为static?
输入样例:
4 rect 3 1 rect 1 5 cir 1 cir 2
输出样例:
38.84 23.700000000000003 [Rectangle [width=3, length=1], Rectangle [width=1, length=5], Circle [radius=1], Circle [radius=2]] class Rectangle,class Shape class Rectangle,class Shape class Circle,class Shape class Circle,class Shape
答案:
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); Shape[] shapes = new Shape[n]; for (int i = 0; i < n; i++) { String str = sc.next(); if("rect".equals(str)){ int length = sc.nextInt(); int width = sc.nextInt(); shapes[i] = new Rectangle(length,width); }else{ int radius = sc.nextInt(); shapes[i] = new Circle(radius); } } System.out.println(sumAllPerimeter(shapes)); System.out.println(sumAllArea(shapes)); System.out.println(Arrays.deepToString(shapes)); for (Shape shape : shapes) { System.out.print(shape.getClass()+","); System.out.println(shape.getClass().getSuperclass()); } } public static double sumAllArea(Shape[] shapes){ double sum = 0; //增强for方式遍历数组 for (Shape shape : shapes) { sum+=shape.getArea(); } return sum; } public static double sumAllPerimeter(Shape[] shapes){ double sum = 0; //增强for方式遍历数组 for (Shape shape : shapes) { sum+=shape.getPerimeter(); } return sum; } } class Rectangle extends Shape{ private int width; private int length; public Rectangle() { } public Rectangle(int width, int length) { this.width = width; this.length = length; } public int getWidth() { return width; } public void setWidth(int width) { this.width = width; } public int getLength() { return length; } public void setLength(int length) { this.length = length; } //长方形的求周长方法 public double getPerimeter(){ return (this.length+this.width)*2; } //长方形的求面积方法 public double getArea(){ return this.length*this.width; } public String toString() { return "Rectangle [width=" + width + ", length=" + length + "]"; } } class Circle extends Shape{ private int radius; private final double PI = 3.14; public Circle() { } public Circle(int radius) { this.radius = radius; } public int getRadius() { return radius; } public void setRadius(int radius) { this.radius = radius; } //圆形的求面积方法 public double getArea(){ return this.radius*this.radius*PI; } //圆形的求周长方法 public double getPerimeter(){ return this.radius*2*PI; } public String toString() { return "Circle [radius=" + radius + "]"; } } abstract class Shape{ public abstract double getArea(); public abstract double getPerimeter(); } #
解析:
可恶啊!没想到最后还是要用多态,继承来做,还不如上一题直接用多态继承算了, 集美大学是吧,我记住了!
居然敢黑我家哥哥。
这题主要考验同学对多态和继承的理解,不过这题没考验instanceof这个关键词我表示惊讶,有兴趣的同学可以了解下。