【问题标题】:Instantiating a class does not work, constructor parameter did not pass实例化一个类不起作用,构造函数参数没有传递
【发布时间】:2018-07-27 20:07:46
【问题描述】:

我试图从我的测试类中初始化一个圆形对象,但参数(5.5)没有通过。结果是错误的。我尝试调试并发现圆形类中的半径为 0.00,5.5 没有传递到圆形类中。 任何人都可以帮助我吗?

这是我的输出:

The area of circle is:  3.14

这是我的测试课:

public class ShapeTest {
        public static void main(String[] args){
            Circle circle = new Circle(5.5);
            System.out.println(circle);
            }


        }
    }

这是我的圈子班:

public class Circle extends TwoDimensionalshape {
        private double radius;

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

    public void setRadius(double radius){
        this.radius = radius;
    }
    public double getRadius(){
        return radius;
    }

    @Override
    public double getArea(){
        return 3.14+getRadius()+getRadius();
    }

    @Override
    public String toString(){
        return String.format("%s %,.2f%n ","The area of circle is: ",getArea());
    }



}

这是我的超级班:

public class TwoDimensionalshape implements Area{
        private double radius;
        private double base;
        private double height;

        public TwoDimensionalshape(double radius){
            this.radius = radius;
    }

    public TwoDimensionalshape(double base, double height){
        this.base = base;
        this.height = height;
    }

    public double getRadius() {
        return radius;
    }


    public double getBase() {
        return base;
    }

    public double getHeight() {
        return height;
    }

    @Override
    public double getArea(){
        return 1;
    }

    public String toString(){
        return "The area is: "+getArea();
    }


}

【问题讨论】:

  • 您在 getArea() 方法中存在数学错误。将 + 替换为 *
  • 我这样做是为了调试,得到半径值后会改回来。 :)

标签: java inheritance parameters constructor polymorphism


【解决方案1】:

Circle 中的radius 变量隐藏TwoDimensionalshape 中的radius 变量。它们是 2 个不同的变量。您的构造函数设置了TwoDimensionalShape 中的那个,但getArea 使用的是Circle 中的那个。

删除Circle 中的radius 变量。通过从Circle 中删除该方法,让Circle 继承getRadius。同样在Circle 中,将setRadius 移动到TwoDimensionalShape

另外,在getArea 中,将半径相乘而不是相加两次。您也可以使用Math.PI 代替3.14

return Math.PI * getRadius() * getRadius();

【讨论】:

  • 我在我的 Circle 构造函数中添加了 this.radius = radius 并且它可以工作。所以我不应该在超类和子类中声明同名的变量,对吧?
  • 不,您不应该在子类中添加同名变量。这是您的问题的根本原因。在构造函数中添加this.radius = radius; 并不是最好的解决方案。你还有 2 个radius 变量。
  • 超类半径变量是私有的。所以即使没有 super 关键字,子类也不能使用它。
【解决方案2】:

从 Circle 类中删除 private double radius;,它应该可以工作。还将 Circle 类中的 getRadius() 方法主体更改为: return super.getRadius();

【讨论】:

  • 我在我的 Circle 构造函数中添加了 this.radius = radius 并且它可以工作。感谢您的帮助:)
【解决方案3】:

Circle 构造函数中缺少以下行:

public Circle(double radius){
    super(radius);
    this.radius = radius; // add this line
} 

由于radiusTwoDimensionalshape 中是private,因此您无法从子类(在这种情况下从Circle)访问它。您需要在Circle 的构造函数中设置Circle 类的radius 的值,如在您的代码中,它使用radiusradius 值计算getArea()

另一个问题 - 要正确计算面积,您应该更改:

return 3.14 + getRadius() + getRadius();

到:

return 3.14 * getRadius() * getRadius();

【讨论】:

  • @lanlan 很高兴我能提供帮助 - 请记住,rgettman 的答案提供了更好的方法,无论如何您可以考虑我的答案作为替代方案。
猜你喜欢
  • 1970-01-01
  • 2014-02-14
  • 2019-09-29
  • 2019-01-30
  • 1970-01-01
  • 2016-06-09
  • 1970-01-01
  • 2015-07-23
  • 1970-01-01
相关资源
最近更新 更多