【问题标题】:Calling variable to subclass from superclass从超类调用变量到子类
【发布时间】:2016-06-10 03:41:41
【问题描述】:

我没有运气从我的超类调用一个变量到我的子类。有人可以帮忙吗?

//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)
    }
  }

代码还有很多,但我遇到的主要问题是在子类中,“面积”变量是 0.00,我不确定如何让它等于计算的“面积”超类

【问题讨论】:

  • 您应该在使用area之前运行getArea()

标签: java inheritance subclass superclass


【解决方案1】:

尝试:

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

另外:圆应该在构造函数中用它的半径初始化,并且没有字段区域,因为它依赖于半径:

public class Circle {

  protected final double radius;

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

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

并且圆锥不是圆的适当子类,圆锥应该有一个字段Circle baseShape

【讨论】:

    【解决方案2】:

    这里的变量区是一个实例变量,所以它的默认值设置为 0.0d。请参阅此link。如果你想改变面积值然后想打电话 getArea() 方法。检查下面的代码,

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

    【讨论】:

      【解决方案3】:

      为超类和子类添加构造函数,如下所示。

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

      之后,就可以使用子类的getVolume()方法了。

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

      【讨论】:

        【解决方案4】:

        您正在使用 protected 将变量继承到子类完全没问题。这是正确的答案

        //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)
            }
          }
        

        【讨论】:

          【解决方案5】:

          除非您将值设置为半径和面积,否则它将保持为 0。我假设您已经设置了值。您应该使用 this 关键字来获取设定值。如果您放置整个代码而不只是将其隐藏为注释,则很容易找到缺陷。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2013-02-25
            • 1970-01-01
            • 1970-01-01
            • 2016-05-27
            • 1970-01-01
            • 2012-03-19
            • 1970-01-01
            相关资源
            最近更新 更多