【问题标题】:trouble understanding classes objects and constructors难以理解类对象和构造函数
【发布时间】:2015-01-07 10:12:20
【问题描述】:

我无法理解类和构造函数的工作方式。 (这是在java中) 我必须创建一个名为 Complex 的类来操作复数,并且该类将有变量 realPart 和 ImaginaryPart 类型为 double。现在如果 c1 和 c2 是 Complex 类型的对象,那么如果我执行 c1.add(c2) 它应该返回 Complex 对象,它是两个对象的总和。但是这是我卡在我不知道如何添加这两个数字的部分?

到目前为止,我已经这样做了:

public static void main(String[] args) {
    Complex c1 = new Complex(2, 8);
    Complex c2 = new Complex(2.3, 5.4);
    Complex c3 = c1.add(c2);
   Complex c4 = Complex.add(c1,c2);

}

}

类复杂{

private double realPart;
private double imaginaryPart;

public Complex() {

}

public Complex(double c1, double c2) {
    realPart = c1;
    imaginaryPart = c2;

}

public void setValue(int numberOne) {
    realPart = numberOne;
}

public Complex add(Complex other) {
    Complex result = new Complex();
    this.realPart = 3;
    return result;

}

public Complex subtract(Complex other) {
    Complex result = new Complex();
    this.realPart = 5;

    return result;
}

public String toString() {

    return c1.add(c2);
}

}

【问题讨论】:

    标签: java class object methods constructor


    【解决方案1】:

    例如,如果您将执行:c1.Add(c2),您的"this" 将是Complex 对象 c1

    public Complex add(Complex other) {
        Complex result = new Complex();
        double realResult = this.realPart + other.realPart;
        double imaginaryResult = this.imaginaryPart + other.imaginaryPart;
        result.realPart = realResult;
        result.imaginaryPart = imaginaryResult;
        return result;
    }
    

    Substruct 方法也一样

    public Complex subtract(Complex other) {
        Complex result = new Complex();
        double realResult = this.realPart - other.realPart;
        double imaginaryResult = this.imaginaryPart - other.imaginaryPart;
        result.realPart = realResult;
        result.imaginaryPart = imaginaryResult;
        return result;
    }
    

    我建议你阅读: https://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html

    【讨论】:

      【解决方案2】:

      首先在新的Complex 中计算出您想要的值,然后根据这些值使用new 来创建新的Complex。最后,使用return 使新的Complex 成为方法调用返回的值。

      这是实现它的一种方法,但不是唯一的方法。请注意,当您编写realPart 而不指定要查找其实部的对象时,它将是当前对象中的realPart - 方法调用中点之前的那个。也就是说,和写this.realPart是一样的。

      public Complex add(Complex other) {
          double realPartOfResult = realPart + other.realPart;
          double imaginaryPartOfResult = imaginaryPart + other.imaginaryPart;
          Complex result = new Complex(realPartOfResult, imaginaryPartOfResult);
          return result;
      }
      

      【讨论】:

        【解决方案3】:

        像这样:

        public Complex add(Complex other) {
            Complex result = new Complex();
            result.realPart = this.realPart + other.realPart;
            result.imaginaryPart = this.imaginaryPart + other.imaginaryPart;
        
            return result;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-04-22
          • 2019-01-17
          • 1970-01-01
          • 2014-05-16
          相关资源
          最近更新 更多