【问题标题】:error in the programme - inheritance程序错误——继承
【发布时间】:2015-05-09 19:11:50
【问题描述】:

我有一个 java 代码,那里似乎有错误;我建议在超类中未定义B类中的构造函数,在B类中未定义方法Circle(radius)。

public class Circle {
    private double redius;

    public Circle (double radius)
    {
        radius = radius;
    }

    public double getRedius() {
        return redius;
    }

    /*public void setRedius(double redius) {
        this.redius = redius;
    }*/

    public double getArea(double radius)
    {
        return radius * radius * Math.PI;
    }
}
     class B extends Circle{
         private double length;

         B(double radius , double length)//this constructor is undefined in the super class
         {
             Circle (radius);// this method is undefined 
             length = length;
         }

         public double getArea()
         {
             return getArea() * length;
         }
     }

【问题讨论】:

  • 你需要检查你的拼写,redius != radius...

标签: inheritance subclass super


【解决方案1】:

使用super(radius); 而不是Circle(radius);

另外,分配length = length 根本没有任何作用。使用this.length = length;length 参数分配给length 实例字段。 (Circle 构造函数中存在类似问题 - 它应该是 redius = radius;(注意您的拼写略有不同),因为参数和实例字段具有不同的名称,因此无需指定您在说的是哪一个关于。Java 编译器不是千里眼。)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-28
    • 1970-01-01
    • 2015-09-27
    • 2015-06-09
    • 2012-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多