【问题标题】:Basic polymorphism/inheritance issues基本的多态性/继承问题
【发布时间】:2015-07-19 19:32:09
【问题描述】:

我正在使用 Java。我已经做了所有我能做的研究,但我找不到我的问题的答案。这段代码的某些部分我不允许更改,但仍然满足作业的要求。我正在与七个班级一起工作。 “形状”类如下所示:

package homework6;

public abstract class Shape{

protected String name;

public String getname(){
    return name;
}

abstract double getSurfaceArea();
}

我不允许更改本课程的任何部分。其他类称为 Circle、Cylinder、Square、Geometric Shape 和 main 类。在每个构造函数中,我被指示“设置名称等于对象的名称”。我对每个班级都这样做了,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;
}

}

这就是问题所在。在我也无法更改的主类中,有一些测试代码。除了“名称”部分之外,我的程序中的所有内容都可以正常工作。在下面的代码中,“getName”带有红色下划线,错误是“找不到符号”。我曾尝试使用“超级”关键字进行试验,但未能成功。我已经为此工作了好几天,我观看了无数的 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());
        }

表面区域显示正确,但我不确定我需要做什么才能显示名称。我无法添加任何不存在的方法或构造函数:名称应该是从已经存在的构造函数中提取的,不知何故。

【问题讨论】:

  • 除了 Anubian Noob 答案之外,我还要补充一点,使用 instanceof 运算符会破坏继承和多态性的目的。您要做的是让Shape 的每个专业化都有其自定义行为(让它成为面积计算或打印详细信息)以避免动态检查运行时类型。
  • 天哪。 Anubian Noob....我不敢相信我犯了这样一个业余的错误。总决赛真的让我大吃一惊。谢谢你,成功了。
  • 事情是这样的——我的老师是编写 main 方法的人,他告诉我们保持原样,用它来确保我们的代码正常工作。我们甚至从未被告知“instanceof”是什么意思,或者它的用途。
  • 希望你的这位老师不要以教编程为生。
  • 判断实例是否为instanceofclass

标签: java inheritance constructor polymorphism super


【解决方案1】:

看看方法:

public String getname(){

你这样称呼它:

s.getName()

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

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


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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-16
    相关资源
    最近更新 更多