第一张截图里画问号的地方怎么理解呢?为什么要用声明为static?为什么sumAllArea和sumAllPerimeter方法要放在主类里呢?

img

img



```java
import java.awt.*;
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();
        sc.nextLine();
        Shape[] shapes=new Shape[n];
        for (int i = 0; i < n; i++) {
            String s=sc.nextLine();
            if (s.equals("rect")){
                shapes[i]=new Rectangle(sc.nextInt(), sc.nextInt());
                sc.nextLine();
            }else if (s.equals("cir")){
                shapes[i]=new Circle(sc.nextInt());
                sc.nextLine();
            }
        }
        System.out.println(sumAllPerimeter(shapes));
        System.out.println(sumAllArea(shapes));
        System.out.println(Arrays.toString(shapes));
        for (Shape s: shapes) {
            System.out.println(s.getClass()+","+s.getClass().getSuperclass());
        }
    }

    public static double sumAllArea(Shape[] ss){
        double ret=0;
        for (Shape s : ss) {
            ret += s.getArea();
        }
    return ret;
    }

    public static double sumAllPerimeter(Shape[] ss){
    double ret=0;
        for (Shape s:ss) {
            ret+=s.getPerimeter();
        }
    return ret;
    }
}

 abstract  class Shape{
    static double PI=3.14;
    public abstract double getPerimeter();
    public abstract double getArea();
}

  class Rectangle  extends  Shape{
    private int width;
    private int length;

    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 2.0*(this.length+this.width);
    }

    public double getArea(){
        return this.length*this.width;
    }

    @Override
    public String toString() {
        return "Rectangle [" +
                "width=" + width +
                ", length=" + length +
                ']';
    }
}

  class Circle extends Shape{
    private int radius;

    public Circle(int radius) {
        this.radius = radius;
    }

    public int getRadius() {
        return radius;
    }

    public void setRadius(int radius) {
        this.radius = radius;
    }
    public double getPerimeter(){
        return 2*PI*this.radius;
    }

    public double getArea(){
        return PI*Math.pow(this.radius,2);
    }

    @Override
    public String toString() {
        return "Circle [" +
                "radius=" + radius +
                ']';
    }
}

```

for循环是遍历数组,那两个方法可以放在主类,你也可以重新定义一个类把他们两个放进去,都一样的