基本 polymorphism/inheritance 个问题

Basic polymorphism/inheritance issues

我正在与 Java 合作。我已经完成了所有可能的研究,但我无法找到我的 question.There 的答案是此代码的某些部分我不允许更改并且仍然满足作业的要求。我正在与七个 classes 一起工作。 Class "Shape" 看起来像这样:

package homework6;

public abstract class Shape{

protected String name;

public String getname(){
    return name;
}

abstract double getSurfaceArea();
}

我不能更改此 class 的任何部分。其他 class 称为圆形、圆柱形、方形、几何形状和主要的 class。在每个构造函数中,我都被指示 "set the name equal to the name of the object"。我对每个 class、EG:

都做了同样的事情
package homework6;

public class Rectangle extends Shape{

public double length;
public double width;

public Rectangle(){
    length = 0;
    width = 0;
    name = "Rectangle";
}

public Rectangle(double l, double w){
    length = l;
    width = w;
    name = "Rectangle";   
}

double getSurfaceArea(){
    return length*width;
}

}

问题来了。在我的主 class 中,我也无法更改,其中有一些测试代码。除了 "name" 部分外,我程序中的所有内容都正常工作。在下面的代码中,"getName" 带有红色下划线,错误是 "cannot find symbol"。我曾尝试使用 'super' 关键字进行试验,但无法使其发挥作用。我已经研究了好几天了,我看了无数的 youtube 视频,但我还是想不通。

public static void main(String args[]) {

    Shape list_of_shapes[] = new Shape[10];

    list_of_shapes[0] = new Circle();         
    list_of_shapes[1] = new Circle(1.5);                                    

    list_of_shapes[2] = new Rectangle();        
    list_of_shapes[3] = new Rectangle(3.5, 2);                            

    list_of_shapes[4] = new Square(); 
    list_of_shapes[5] = new Square(4.5);   

    list_of_shapes[6] = new Cylinder();         
    list_of_shapes[7] = new Cylinder(1.5, 2);   


    boolean skipLine = false;
    for(Shape s : list_of_shapes)
    {

        if(s instanceof Circle && !(s instanceof Cylinder))
        {
            System.out.println("Object of class " + s.getName() + " with radius = " + ((Circle)(s)).radius + ",");
            System.out.println("has an area of " + s.getSurfaceArea());
        }
        else if (s instanceof Rectangle && !(s instanceof Square))
        {
            System.out.println("Object of class " + s.getName() + " with length = " + ((Rectangle)(s)).length + " and width = " + ((Rectangle)(s)).width + ",");
            System.out.println("has an area of " + s.getSurfaceArea());
        }
        else if (s instanceof Square)
        {
            System.out.println("Object of class " + s.getName() + " with length = " + ((Square)(s)).length + " and width = " + ((Square)(s)).width + ",");
            System.out.println("has an area of " + s.getSurfaceArea());
        }
        else if (s instanceof Cylinder)
        {
           System.out.println("Object of class " + s.getName() + " with radius = " + ((Cylinder)(s)).radius + " and height = " + ((Cylinder)(s)).height + ",");
            System.out.print("has an area of " + s.getSurfaceArea());
            System.out.println(" and a volume of " + ((Cylinder)(s)).getVolume());
        }

表面积正确显示,但我不确定我需要做什么才能显示名称。我不能添加任何尚不存在的方法或构造函数:名称应该以某种方式从已经存在的构造函数中提取。

看看方法:

public String getname(){

你是这样称呼它的:

s.getName()

Java,与许多语言一样,区分大小写。

我认为这只是库的错字(它 应该 getName,但如果它是 getname 就用那个)。


此外,在您的 Rectangle 构造函数中:

public Rectangle(){
    length = 0;
    width = 0;
    name = "Rectangle";
}

您可以只使用this 来调用另一个构造函数。它只会使您的代码更简单并删除重复的功能。

public Rectangle() {
    this(0, 0)
}