Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java类层次结构从2级以下获取字符串_Java - Fatal编程技术网

java类层次结构从2级以下获取字符串

java类层次结构从2级以下获取字符串,java,Java,我是一名高中生,通过codeHS参加AP java课程,iv'e遇到了一个障碍,我比老师领先了2年,codeHS是一个很好的网站,但它不能解释一切,所以这是我的问题 public abstract class Solid { private String myName; public Solid(String name) { myName = name; } public String getName() { r

我是一名高中生,通过codeHS参加AP java课程,iv'e遇到了一个障碍,我比老师领先了2年,codeHS是一个很好的网站,但它不能解释一切,所以这是我的问题

public abstract class Solid
{
    private String myName;

    public Solid(String name)
    {
        myName = name;
    }

    public String getName()
    {
        return myName;
    }

    public abstract double volume();

    public abstract double surfaceArea();
}



public class Cube extends Solid
{

    public int side;

    public Cube(String name, int side)
    {
        super(name);
        this.side = side;
    }

    public double volume()
    {
        return Math.pow(side, 3);
    }

    public double surfaceArea()
    {
        return 6 * Math.pow(side, 2);
    }

}


public class RectangularPrism extends Cube
{
    public int length;
    public int width;
    public int height;

    public RectangularPrism(String name, int width, int height, int length)
    {
        super(name);
        this.width = width;
        this.height = height;
        this.length = length;
    }


    public double surfaceArea()
    {
        return 2 * (width * length + height * length+ height * width)
    }
}

我的问题是在RectangularPrism类中,构造器,它不是从超类中获取名称,它是多维数据集,我不能/不知道如何将实体类中的名称存储到多维数据集类中,以便从那里获取它?或者有没有一种方法可以从矩形棱柱体类内部的solid类中获取它
RectangularPrism
类是从
Cube
类扩展而来的,但是
Cube
类没有默认构造函数,只有一个自定义构造函数。因此,
RectangularPrism
类必须超越构造函数,代码如下:

public class RectangularPrism extends Cube {

    public int length;
    public int width;
    public int height;

    public RectangularPrism(String name, int width, int height, int length){
        super(name, 0);
        this.width = width;
        this.height = height;
        this.length = length;
    }


    public double surfaceArea()
    {
        return 2 * (width * length + height * length+ height * width);
    }

    @Override
    public double volume() {
        return (width * length * height);
    }
}

现在,您可以使用
getName
方法来获取名称。

其他信息:我知道如果我只通过solid扩展我的矩形棱柱体类,它会起作用,但练习告诉我“使用立方体扩展矩形棱柱体”。太棒了,谢谢c:,是的,这是视频中没有教过的,我假设“super(name,0)”0表示层次结构中的最高类,其中as“super(name)”仅表示上面的一个类,在本例中为多维数据集类谢谢您的赞扬^_^