在类层次结构中设置变量

时间:2016-01-07 02:16:55

标签: java class variables inheritance hierarchy

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

这是主要课程:

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());
    }

  }

}

这里是我要改变的变量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;
  }

}

这是我编写的类,试图访问抽象类中的变量:

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的值时,它会为表面区域和体积返回0.0。我做错了什么?

编辑:这是Cube类:

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;
  }

 }

1 个答案:

答案 0 :(得分:0)

尝试在PrismSet中修改此方法

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

并传递shDriver

还将ShapeDriver类更改为:

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;
    }

}

PrismSet

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;
    }

}