程序中的错误 - 继承

时间:2015-05-09 19:11:50

标签: inheritance subclass super

我有一个java代码,它似乎是错误的;我建议在超类中未定义B类中的构造函数,并且在B类中未定义方法Circle(半径)。

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;
         }
     }

1 个答案:

答案 0 :(得分:0)

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

此外,作业length = length根本不做任何事情。使用this.length = length;length参数分配给length实例字段。 (在Circle构造函数中存在类似的问题 - 它应该是redius = radius;(小心你的略有不同的拼写),这是有效的,因为参数和实例字段有不同的名称,因此无需指定哪一个你在谈论.Java编译器并非透视。)

相关问题