Java 带超()

Java 带超(),java,class,math,methods,Java,Class,Math,Methods,我正在尝试编写一个ComplexNumber类,该类从ImaginaryNumber类继承数据和方法。所以在我的想象数字课上我有这个 public class ImaginaryNumber { //Declaring a variable. protected double coefficient; //Default constructor. public ImaginaryNumber( ) { this.coefficient = 1; }

我正在尝试编写一个ComplexNumber类,该类从ImaginaryNumber类继承数据和方法。所以在我的想象数字课上我有这个

public class ImaginaryNumber 
{
  //Declaring a variable.
  protected double coefficient;

  //Default constructor.
  public ImaginaryNumber( )
  {
      this.coefficient = 1;
  }

  //Parameterized constructor.
  public ImaginaryNumber(double number)
  {
      this.coefficient = number;
  }

  //Adding and returing an imaginary number.
  public ImaginaryNumber add (ImaginaryNumber another)
  {
      return new ImaginaryNumber(this.coefficient + another.coefficient);
  }//More Codes
public class ComplexNumber extends ImaginaryNumber
{
  private double realCoefficient;

  public ComplexNumber ( )
  {
      super ( );
      this.realCoefficient = 1;
  }

  public ComplexNumber (double realNum, double IM)
  {
      super (IM);
      this.realCoefficient = realNum;
  }

  public ComplexNumber add (ComplexNumber a, ComplexNumber b)
 {
    return new ComplexNumber (this.realCoefficient + a.realCoefficient,  super(b) + super(IM)); //I am confused on what to do here.
 }//More Codes
在我的ComplexNumber课程中,我有这个

public class ImaginaryNumber 
{
  //Declaring a variable.
  protected double coefficient;

  //Default constructor.
  public ImaginaryNumber( )
  {
      this.coefficient = 1;
  }

  //Parameterized constructor.
  public ImaginaryNumber(double number)
  {
      this.coefficient = number;
  }

  //Adding and returing an imaginary number.
  public ImaginaryNumber add (ImaginaryNumber another)
  {
      return new ImaginaryNumber(this.coefficient + another.coefficient);
  }//More Codes
public class ComplexNumber extends ImaginaryNumber
{
  private double realCoefficient;

  public ComplexNumber ( )
  {
      super ( );
      this.realCoefficient = 1;
  }

  public ComplexNumber (double realNum, double IM)
  {
      super (IM);
      this.realCoefficient = realNum;
  }

  public ComplexNumber add (ComplexNumber a, ComplexNumber b)
 {
    return new ComplexNumber (this.realCoefficient + a.realCoefficient,  super(b) + super(IM)); //I am confused on what to do here.
 }//More Codes
我被困的部分是加法。我不知道如何从ImaginaryNumber类调用方法来处理虚部。我假设我必须以某种形式使用super()。但是我不知道调用方法的正确语法


也只是为了再次检查。我在ImaginaryNumber类中调用构造函数是否正确?

我想是的,通过这样做,您希望实现复数的加法。因此,在ImaginaryNumber类中,您必须有一个返回系数的方法

  public double getImaginaryCoefficient() {
     return this.coefficient;
  }
现在,您可以调用getImaginaryCofficient()方法并执行您想要执行的任何操作

像这样的,

public ComplexNumber add (ComplexNumber a, ComplexNumber b)
 {
    return new ComplexNumber (this.realCoefficient + a.realCoefficient,  a.getImaginaryCoefficient()+b.getImaginaryCoefficient()); //I am confused on what to do here.
 }//More Codes

@谢谢,我不知道这么简单。但是,现在我收到一个错误,说它找不到IM,但我不是在参数化构造函数中声明了IM吗?
super.add(…)
?(为了完整性再次添加注释)。这是因为
super(IM)
<代码>IM仅在构造函数中已知。考虑添加另一个变量,如
private double IM
和构造函数中的
this.IM=IM。(就像你用
realcefficient
做的那样)@pzaenger我明白了。我从这里开始。谢谢你的帮助!