java - 实现 Java 可克隆接口(interface)

标签 java interface cloneable

我不确定如何在我的 Complex 类中实现 Cloneable 接口(interface)。我已经实现了 Comparable,但我似乎无法弄清楚 Cloneable。我有以下示例代码,我正在使用它来尝试理解它。我知道它应该类似于 public Complex clone () { then super.clone() ,我认为它返回 new Complex (实部和虚部),但我不太确定。关于我将如何实现有什么建议吗?

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

}

最佳答案

像这样实现,它将返回 Complex 对象的克隆。如果您需要不同的东西,那么不要打电话 super.clone()使用您需要的任何逻辑自行创建复杂对象。

@Override
protected Object clone() throws CloneNotSupportedException {
    return super.clone();
}

关于java - 实现 Java 可克隆接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35962237/

相关文章:

java - 我如何使用来自 firebase 的数据制作一个可点击的 recyclerview

java - java中的泛型,扩展接口(interface)

generics - .NET 4 是否提供了解决这个简单通用接口(interface)问题的方法?

java - 用 Java 实现一副纸牌

java - ANR 输入调度在 Android 4.4 上超时

java - Spring BeanFactory 是如何实例化一个非公共(public)类的?

java - 从 Jdialog 传递到 Jframe

javascript - AngularJS 中的接口(interface)

java: 克隆方法冲突

java - 重写 Java 中的 Clone() 方法