在 class 层次结构中设置变量

Setting variables in class hierarchy

我正在编写一个程序,根据学校的 class 层次结构查找形状的体积和表面积。我试图在主 class 中获取一个方法来设置抽象 class 中的变量,但它不起作用。

这是主要内容 class:

import java.util.Scanner;

public class ShapeDriver{

  public double cubeSide;

  public static void main(String[] args){

    Scanner input = new Scanner(System.in);
    System.out.println("Enter 1 for cube, 2 for rectangular prism, 3 for cylinder, 4 for cone, or 5 for sphere.");
    int shapeNum = input.nextInt();

    if(shapeNum == 1){
      ShapeDriver shDriver = new ShapeDriver();
      PrismSet prism = new PrismSet();
      System.out.println("Enter the length of a side of the cube.");
      shDriver.cubeSide = input.nextDouble();
      prism.setCubeLength();

      Cube cu = new Cube();
      System.out.println("Surface area: " + cu.surfaceArea());
      System.out.println("Volume: " + cu.volume());
    }

  }

}

这是摘要 class,其中包含我要更改的变量 prismWidth 和 prismHeight:

public abstract class Prisms implements ShapeFunctions{

  public double prismWidth;
  public double prismHeight;
  public double prismDepth;

  public double findFrontBackFaceArea(double prismHeight, double prismWidth){
    double prismFrontBackFaceArea = prismHeight * prismWidth * 2;
    return prismFrontBackFaceArea;
  }

  public double findRightLeftFaceArea(double prismHeight, double prismDepth){
    double prismRightLeftFaceArea = prismHeight * prismWidth * 2;
    return prismRightLeftFaceArea;
  }

  public double findTopBottomFaceArea(double prismWidth, double prismDepth){
    double prismTopBottomFaceArea = prismWidth * prismDepth * 2;
    return prismTopBottomFaceArea;
  }

}

这是我写的 class 尝试访问摘要中的变量 class:

public class PrismSet extends Prisms{

  public double surfaceAreaHolder;
  public double volumeHolder;

  public void setCubeLength(){
    ShapeDriver sDriver = new ShapeDriver();
    super.prismHeight = sDriver.cubeSide;
    super.prismWidth = sDriver.cubeSide;
  }

  public double surfaceArea(){
    return surfaceAreaHolder;
  }

  public double volume(){
    return volumeHolder;
  }


}

当我输入一个数字作为 cubeSide 的值时,表面积和体积都为 returns 0.0。我做错了什么?

编辑:这是立方体 class:

public class Cube extends Prisms implements ShapeFunctions{

  public double surfaceArea(){
    double cubeSurfaceArea = super.findFrontBackFaceArea(super.prismHeight, super.prismWidth) * 3;
    return cubeSurfaceArea;
  }

  public double volume(){
    double cubeVolume = Math.pow(super.prismWidth, 3);
    return cubeVolume;
  }

 }

尝试在PrismSet中修改此方法

public void setCubeLength(ShapeDriver sDriver){
 super.prismHeight = sDriver.cubeSide;
 super.prismWidth = sDriver.cubeSide;
}

并将其传递给 shDriver


同时将 ShapeDriver class 更改为:

public static void main(String[] args){

    Scanner input = new Scanner(System.in);
    System.out.println("Enter 1 for cube, 2 for rectangular prism, 3 for cylinder, 4 for cone, or 5 for sphere.");
    int shapeNum = input.nextInt();

    if(shapeNum == 1){
      ShapeDriver shDriver = new ShapeDriver();
      Cube cube = new Cube();
      System.out.println("Enter the length of a side of the cube.");
      shDriver.cubeSide = input.nextDouble();
      cube.setCubeLength(shDriver); // change this also

      System.out.println("Surface area: " + cu.surfaceArea());
      System.out.println("Volume: " + cu.volume());
    }

  }

好的,所以,我认为您希望您的 Cube 扩展 PrismSet,否则,我不确定您想要实现什么。 我设法使用这段代码来实现它:

import java.util.Scanner;

public class ShapeDriver {

    public double cubeSide;

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        System.out.println("Enter 1 for cube, 2 for rectangular prism, 3 for cylinder, 4 for cone, or 5 for sphere.");
        int shapeNum = input.nextInt();

        if (shapeNum == 1) {
            ShapeDriver shDriver = new ShapeDriver();
            Cube cube = new Cube();
            System.out.println("Enter the length of a side of the cube.");
            shDriver.cubeSide = input.nextDouble();
            cube.setCubeLength(shDriver);

            System.out.println("Surface area: " + cube.surfaceArea());
            System.out.println("Volume: " + cube.volume());
        }
    }
}

棱镜:

public abstract class Prisms implements ShapeFunctions {

    public double prismWidth;
    public double prismHeight;
    public double prismDepth;

    public double findFrontBackFaceArea(double prismHeight, double prismWidth) {
        double prismFrontBackFaceArea = prismHeight * prismWidth * 2;
        return prismFrontBackFaceArea;
    }

    public double findRightLeftFaceArea(double prismHeight, double prismDepth) {
        double prismRightLeftFaceArea = prismHeight * prismWidth * 2;
        return prismRightLeftFaceArea;
    }

    public double findTopBottomFaceArea(double prismWidth, double prismDepth) {
        double prismTopBottomFaceArea = prismWidth * prismDepth * 2;
        return prismTopBottomFaceArea;
    }

}

棱镜组

public class PrismSet extends Prisms {

    public double surfaceAreaHolder;
    public double volumeHolder;

    public void setCubeLength(ShapeDriver sDriver) {
        super.prismHeight = sDriver.cubeSide;
        super.prismWidth = sDriver.cubeSide;
    }

    public double surfaceArea() {
        return surfaceAreaHolder;
    }

    public double volume() {
        return volumeHolder;
    }

}

立方体

public class Cube extends PrismSet implements ShapeFunctions {

    public double surfaceArea() {
        double cubeSurfaceArea = super.findFrontBackFaceArea(super.prismHeight, super.prismWidth) * 3;
        return cubeSurfaceArea;
    }

    public double volume() {
        double cubeVolume = Math.pow(super.prismWidth, 3);
        return cubeVolume;
    }

}