这段代码适合初学者吗?我怎样才能使它更简洁? (条件是:我们必须使用它来演示 OOPs 概念)

Is this code good as a beginner? How can i make it more concise? (Condition is that: We have to demonstrate OOPs Concept using this)

这段代码适合初学者吗?如果没有,我怎样才能使它更简洁?如果有,还有什么建议吗?

(条件是:我们必须使用它来演示 OOPs 概念)

关于代码:这是一个 java 程序,它通过制作一个关于 3D 测量的程序来演示 OOP 功能(初学者级别)的使用,在该程序中用户必须 select 塑造然后输入测量作为根据形状要求打印曲面面积、体积等

这是我的代码:

import java.util.*;
import java.lang.*;

class Mensuration {

    public static int selectShape(){
        System.out.print("1. Cube\n2. Cuboid\n3. Cylinder\n4. Cone\n5. Sphere\n6. Hemisphere\nSelect the Shape to perform action upon: ");
        Scanner selectShape = new Scanner(System.in);
        return selectShape.nextInt();
    }
}

class singleInputShapes {
    static float getInput() {
        Scanner inp = new Scanner(System.in);
        return inp.nextFloat();
    }
}

class Cube extends singleInputShapes{
    private float side;

    public float CSA() {
        System.out.print("Enter Side: ");
        this.side = singleInputShapes.getInput();
        return 4*side*side;
    }

    public float TSA() {
        return 6*side*side;
    }

    public float Volume() {
        return side*side*side;
    }


}

class Cuboid {
    private final float l;
    private final float b;
    private final float h;

    Cuboid() {
        System.out.print("Enter Length: ");
        this.l = singleInputShapes.getInput();
        System.out.print("Enter Breadth: ");
        this.b = singleInputShapes.getInput();
        System.out.print("Enter Height: ");
        this.h = singleInputShapes.getInput();

    }

    public float CSA() {
        return 2*h*(l+b);
    }

    public float TSA() {
        return 2*(l*b+b*h+h*l);
    }

    public float Volume() {
        return l*b*h;
    }
}

class Cylinder {
    private final float r;
    private final float h;

    Cylinder() {
        System.out.print("Enter radius: ");
        this.r = singleInputShapes.getInput();
        System.out.print("Enter Height/Length: ");
        this.h = singleInputShapes.getInput();
    }
    public float CSA() {
        return (float) (2*Math.PI*r*h);
    }

    public float TSA() {
        return (float) (2*Math.PI*r*(r+h));
    }

    public float Volume() {
        return (float) (Math.PI*r*r*h);
    }
}

class Cone {
    private final float r;
    private final float h;

    Cone() {
        System.out.print("Enter radius: ");
        this.r = singleInputShapes.getInput();
        System.out.print("Enter Height/Length: ");
        this.h = singleInputShapes.getInput();
    }
    public float slantHeight() {
        return (float) Math.sqrt(r*r + h*h);
    }

    public float CSA() {
        return (float) (Math.PI*r*slantHeight());
    }

    public float TSA() {
        return (float) (Math.PI*r*(r+slantHeight()));
    }

    public float Volume() {
        return (float) (Math.PI*r*r*h/3);
    }
}

class Sphere extends singleInputShapes{
    private float radius;

    public double CSA() {
        System.out.print("Enter Radius: ");
        this.radius = singleInputShapes.getInput();
        return 4*Math.PI*radius*radius;
    }
    public double HemiTSA() {
        return 3*Math.PI*radius*radius;
    }
    public float Volume() {
        return (float) (4*Math.PI*radius*radius*radius/3);
    }
}

public class Main {

    public static void main(String[] args) {
        int shapeNumber = Mensuration.selectShape();
        switch (shapeNumber) {
            case 1 -> {
                Cube c1 = new Cube();
                System.out.println("- - - - - Cube - - - - -");
                System.out.println("Curved Surface Area of Cube is: " + c1.CSA());
                System.out.println("Total Surface Area of Cube is: "+ c1.TSA());
                System.out.println("Volume of Cube is: "+ c1.Volume());
            }
            case 2 -> {
                System.out.println("- - - - - Cuboid - - - - -");
                Cuboid cd1 = new Cuboid();
                System.out.println("Curved Surface Area of Cuboid is: " + cd1.CSA());
                System.out.println("Total Surface Area of Cuboid is: "+ cd1.TSA());
                System.out.println("Volume of Cuboid is: "+ cd1.Volume());
            }
            case 3 -> {
                System.out.println("- - - - - Cylinder - - - - -");
                Cylinder cy1 = new Cylinder();
                System.out.println("Curved Surface Area of Cylinder is: " + cy1.CSA());
                System.out.println("Total Surface Area of Cylinder is: "+ cy1.TSA());
                System.out.println("Volume of Cylinder is: "+ cy1.Volume());
            }
            case 4 -> {
                System.out.println("- - - - - Cone - - - - -");
                Cone co1 = new Cone();
                System.out.println("Slant height of Cone is: " + co1.slantHeight());
                System.out.println("Curved Surface Area of Cone is: " + co1.CSA());
                System.out.println("Total Surface Area of Cone is: "+ co1.TSA());
                System.out.println("Volume of Cylinder is: "+ co1.Volume());
            }
            case 5 -> {
                System.out.println("- - - - - Sphere - - - - -");
                Sphere s1 = new Sphere();
                System.out.println("Surface Area of Sphere is: " + s1.CSA());
                System.out.println("Volume of Sphere is: "+ s1.Volume());
            }
            case 6 -> {
                System.out.println("- - - - - Hemisphere - - - - -");
                Sphere s1 = new Sphere();
                System.out.println("Curved Surface Area of HemiSphere is: " + s1.CSA()/2);
                System.out.println("Total Surface Area of HemiSphere is: " + s1.HemiTSA());
                System.out.println("Volume of HemiSphere is: "+ s1.Volume()/2);
            }
        }
    }
}

在我看来,对于初学者来说,你需要更多的说明才能让他们更容易理解。 最初,可以提出几个不应用 (OOP) 原理的过程编程形式的示例,以便他们了解它的好处。

为了改进当前代码,我将把它分成几个部分:

1 - 主要是:

会在理论方面对初学者说的好处之一是它减少了每个过程中的重复。 但是当你看到main函数的时候,很多语句都是重复的,并没有应用一个漂亮的原则,就是(DRY: "do not repeat yourself").

注意以下句子在每个几何形状中的重复,例如名称和 (的曲面面积)和(的体积)如果有一个函数并且根据形状实现了例外,那么最好将连接或其他手动技术应用于字符串。

2 - 在 类:

  • 对于初学者来说,有些东西是第一次见(OOP)是无法理解的,比如private。
  • OOP 原理理解每个几何形状或更准确地说每个对象都有自己的值所以为什么使用 (final) 我记得它只用于常量但是现成的例子被使用像 (Math.PI) 在等式中。
  • 忘记了为什么代表(CSA)的命名方法,如果写的完整,就是(curved_Surface_Area),初学者会更清楚,更注重理解这个的原理方法,不要被缩写的意思弄糊涂了,如果还有,会加评论。

整体来说,从名家到新手,都算是一个漂亮且不同的例子,我想他们在以后的作品中应用原理时会记住它,祝你好运:)