多级继承不起作用

时间:2013-11-25 04:51:18

标签: java inheritance

我在学校的实验室工作,我几乎已经完成了,但是有一部分我无法上班。除了进入Cube之外,继承有效。由于某种原因,它不会计算面积或体积(它只是0)。我认为从Square到Cube继承的方式存在问题。帮助太棒了!

package InheritanceTest;

import javax.swing.JOptionPane;

public class InheritanceTest {

    public static void main(String[] args) {
        String input = "";
        Point point = new Point();

        input = getinput("Set variable X");
        point.setx(input);
        input = getinput("Set variable Y");
        point.sety(input);
        System.out.println("Point, x = " + point.getx() + " y = " + point.gety());

        Square square = new Square();
        input = getinput("Set variable Side Length");
        square.setSideLength(input);
        System.out.println("Square, x = " + point.getx() + " y = " + point.gety()
                + " Area = " + square.getAreaOfSquare() + " Perimeter = "
                + square.getPerimeterOfSquare());

        Cube cube = new Cube();
        input = getinput("Set variable depth");
        cube.setDepth(input);
        System.out.println("cube, x = " + point.getx() + " y = " + point.gety()
                + " Depth = " + cube.getDepth() + " Area = " + cube.getAreaOfCube()
                + " Volume = " + cube.getVolumeOfCube());
    }

    private static String getinput(String string) {
        String x = JOptionPane.showInputDialog(string);
        return x;
    }
}
package InheritanceTest;

public class Cube extends Square {

    private int depth;

    Cube() {
        super();
        depth = 0;
    }

    Cube(int x, int y, int sideLength, int d) {
        super(x, y, sideLength);
        this.depth = d;
    }

    public int getAreaOfCube() {
        return (6 * sideLength * sideLength);
    }

    public int getVolumeOfCube() {
        return (sideLength * sideLength * sideLength);
    }

    public String getDepth() {
        return Integer.toString(depth);
    }

    public void setDepth(String i) {
        depth = Integer.parseInt(i);
    }
}
package InheritanceTest;

public class Point {

    private int x;
    private int y;

    Point() {
        x = 0;
        y = 0;
    }

    Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public String getx() {
        return Integer.toString(x);
    }

    public String gety() {
        return Integer.toString(y);
    }

    public void setx(String input) {
        x = Integer.parseInt(input);
    }

    public void sety(String input) {
        y = Integer.parseInt(input);
    }
}
package InheritanceTest;

public class Square extends Point {

    protected int sideLength;

    Square() {
        super();
        sideLength = 0;
    }

    Square(int x, int y, int l) {
        super(x, y);
        this.sideLength = l;
    }

    public int getAreaOfSquare() {
        return sideLength * sideLength;
    }

    public int getPerimeterOfSquare() {
        return sideLength + sideLength;
    }

    public String getSideLength() {
        return Integer.toString(sideLength);
    }

    public void setSideLength(String input) {
        sideLength = Integer.parseInt(input);
    }
}

2 个答案:

答案 0 :(得分:2)

当您创建立方体(新立方体())时,您不会为其延伸的方形对象设置边长(或x和y)。

Cube(){
    // This is the constructor called.
    super();
    depth = 0;
}

Cube(int x, int y, int sideLength, int d){
    super(x, y, sideLength);
    this.depth = d;
}

您可能希望将x,y和长度值提取到变量中并使用" new Cube(x,y,length,depth)"

如下所示

    String x = getinput("Set variable X");
    String y = getinput("Set variable Y");
    String sideLength = getinput("Set variable Side Length");
    String depth getinput("Set variable depth");

    Cube cube = new Cube(x, y, sideLength, depth);

答案 1 :(得分:0)

看看如何定义getVolumeOfCube()。您正在使用sideLength计算音量,但您从未将sideLength设置为任何非零值。将sideLength更改为深度,您将获得所需的值。