繁体   English   中英

实现 Java 可克隆接口

[英]Implementing the Java Cloneable Interface

我不确定如何在我的 Complex 类中实现 Cloneable 接口。 我已经实现了 Comparable,但我似乎无法弄清楚 Cloneable。 我有以下示例代码,我用它来尝试了解它。 我知道它应该类似于 public Complex clone () { 然后 super.clone() 并且我认为它返回新的 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()使用您需要的任何逻辑自行创建 Complex 对象。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM