简体   繁体   English

从超类调用变量到子类

[英]Calling variable to subclass from superclass

I'm having no luck calling a variable from my superclass to my subclass. 我没有从我的超类到子类调用变量的运气。 Can anyone help out? 有人可以帮忙吗?

//SUPERCLASS
public class Circle {

  protected double radius;
  protected double area;

  //Some code to construct object and initialize radius

  //Return Calculated Area
  protected double getArea() {
    area = Math.pow(radius, 2) * Math.PI;
    return area;
  }

}

//SUBCLASS
public class Cone extends Circle {

  private double height;

//Some more code with constructors and different methods

  public double getVolume() {
    {
      return (area * height / 3)
    }
  }

There is a lot more to the code but the main problem I'm having is within the subclass, the 'area' variable is 0.00 and I'm unsure how to get it equal to the 'area' that is calculated in the superclass 代码还有很多,但是我遇到的主要问题是在子类中,'area'变量为0.00,我不确定如何使其等于超类中计算的'area'

try: 尝试:

public double getVolume() {
   return (getArea() * height / 3)
}

Besides: A circle should be initialized with its radius in the constructor and not have a field area, because it is dependent on the radius: 此外:圆应在构造函数中以其半径初始化,并且没有字段区域,因为它取决于半径:

public class Circle {

  protected final double radius;

  public Circle(double radius) {
    this.radius = radius
  }

  public double getArea() {
    return Math.pow(radius, 2) * Math.PI;
  }
}

And a cone is not a proper sub class of a circle, the cone should have a field Circle baseShape . 圆锥不是圆的适当子类,圆锥应具有一个字段Circle baseShape

Here variable area is an instance variable so it's default value is set to 0.0d. 这里的变量区域是一个实例变量,因此其默认值设置为0.0d。 Refer to this link . 请参考此链接 If you want to change the area value then want to call getArea() method. 如果要更改区域值,则要调用getArea()方法。 Check below code, 检查下面的代码,

public double getVolume() {
      return (getArea()* height / 3)
}

You are using protected its totally fine to inherit the variable to subclass . 您正在使用protected完全可以将变量继承给subclass。 here is the correct answer 这是正确的答案

//SUPERCLASS
public class Circle {

  protected double radius;
  protected double area;

  //Some code to construct object and initialize radius

  //Return Calculated Area
  protected double getArea() {
    area = Math.pow(radius, 2) * Math.PI;
    return area;
  }

}

//SUBCLASS
public class Cone extends Circle {

  private double height;

//Some more code with constructors and different methods

  public double getVolume() {
    {
      return (getArea() * height / 3)
    }
  }

Unless you set the value to radius and area, it remains 0. I assume you've set the value. 除非将值设置为“半径和面积”,否则它将保持为0。我认为您已经设置了该值。 You should use this keyword to get the set value. 您应该使用关键字来获取设置值。 It would be easy to find the flaw if you put the entire code and not just hiding it as a comment. 如果您将整个代码放入而不只是将其隐藏为注释,则很容易发现该缺陷。

Add Constructors for both super class and sub-class like the following. 为父类和子类添加构造函数,如下所示。

//Super Class
public class Circle {

  protected double radius;
  protected double area;

  public Circle(double radius) {
    this.radius = radius;
    this.area = getArea();
  }

  protected double getArea() {
    area = Math.pow(radius, 2) * Math.PI;
    return area;
  }
}

//Sub Class
public class Cone extends Circle {

private double height;

public Cone(double radius, double height) {
    super(radius);
    this.height = height;
}

public double getVolume() {
    {
      return (area * height / 3);
    }
  }
}

After that, you can use getVolume() method of sub-class. 之后,您可以使用子类的getVolume()方法。

public class Test {

  public static void main(String[] args) {
    Cone c = new Cone(3.0,5.0);
    System.out.println(c.getVolume());
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM