首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >实现Java Cloneable接口

实现Java Cloneable接口
EN

Stack Overflow用户
提问于 2016-03-13 03:22:30
回答 1查看 1.7K关注 0票数 1

我不确定如何在我的复杂类中实现Cloneable接口。我已经实现了可比较的,但我就是想不出可克隆的。我有下面的示例代码,我正在使用它来试着理解它。我知道它应该类似于public Complex clone () { then super.clone(),我认为它会返回新的复数(实部和虚部),但我不太确定。关于我将如何实现,有什么建议吗?

代码语言:javascript
复制
import java.util.Comparator;
import java.util.Scanner;

public class Complex implements Cloneable, Comparable < Complex > {
  private double real;
  private double imag;

  public Complex(double real, double imag) {
    this.real = real;
    this.imag = imag;
  }

  public Complex(double real) {
    this.real = real;
  }

  public Complex() {

  }

  public void setReal(double real) {
    this.real = real;
  }

  public void setImag(double imag) {
    this.imag = imag;
  }

  public double getReal() {
    return real;
  }

  public double getImag() {
    return imag;
  }

  public void add(Complex num1, Complex num2) {
    this.real = num1.real + num2.real;
    this.imag = num1.imag + num2.imag;

  }

  public Complex subtract(Complex num) {
    Complex a = this;
    double real = a.real - num.real;
    double imag = a.imag - num.imag;
    return new Complex(real, imag);
  }

  public Complex multiply(Complex num) {
    Complex a = this;
    double real = a.real * num.real - a.imag * num.imag;
    double imag = a.real * num.imag + a.imag * num.real;
    return new Complex(real, imag);
  }

  public Complex divide(Complex c1, Complex c2) {
    return new Complex((c1.real * c2.real + c1.imag * c2.imag) / (c2.real * c2.real + c2.imag * c2.imag), (c1.imag * c2.real - c1.real * c2.imag) / (c2.real * c2.real + c2.imag * c2.imag));
  }

  public double absolute() {
    return Math.sqrt(real * real + imag * imag);
  }

  public String toString() {
    return this.real + " + " + this.imag + "i";
  }

  /*
   * @Override public Complex clone() throws CloneNotSupportedException {
   * super.clone(); return new Complex(real, imag); }
   */

  public static void main(String[] args) {

    Scanner in = new Scanner(System.in);
    System.out.print("Enter the first set of complex numbers respectively: ");
    double a = in .nextDouble();
    double b = in .nextDouble();

    Complex c1 = new Complex(a, b);

    System.out.print("Enter the second set of complex numbers respectively: ");
    double c = in .nextDouble();
    double d = in .nextDouble();

    Complex c2 = new Complex(c, d);

    Complex result = new Complex(c, d);
    result.add(c1, c2);

    System.out.println("(" + a + " + " + b + "i) + (" + c + " + " + d + "i) = " + result.toString());
    System.out.println("(" + a + " + " + b + "i) - (" + c + " + " + d + "i) = " + c1.subtract(c2));
    System.out.println("(" + a + " + " + b + "i) * (" + c + " + " + d + "i) = " + c1.multiply(c2));
    System.out.println("(" + a + " + " + b + "i) / (" + c + " + " + d + "i) = " + result.divide(c1, c2).toString());
    System.out.println("|" + a + " + " + b + "i| = " + c1.absolute());

    System.out.println(
      "The Comparision of (" + a + " + " + b + "i) AND (" + c + " + " + d + "i) is " + c1.compareTo(c2));

  }

  @
  Override
  public int compareTo(Complex other) {
    int realCompare = Double.compare(getReal(), other.getReal());
    if (realCompare != 0) {
      return realCompare;
    }
    return Double.compare(getImag(), other.getImag());
  }

}
EN

回答 1

Stack Overflow用户

发布于 2016-03-13 03:32:53

像这样实现,它将返回复杂对象的克隆。如果你需要一些不同的东西,那么不要调用super.clone(),而是用你需要的逻辑自己创建复杂的对象。

代码语言:javascript
复制
@Override
protected Object clone() throws CloneNotSupportedException {
    return super.clone();
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35962237

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档