sun_ge_ge_ 2022-04-17 22:05 采纳率: 93.3%
浏览 25
已结题

第一张截图里画问号的地方怎么理解呢?为什么要用声明为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 +
                ']';
    }
}

```

  • 写回答

1条回答 默认 最新

  • 离陌lm 2022-04-17 23:39
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 4月25日
  • 已采纳回答 4月17日
  • 创建了问题 4月17日

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题