繁体   English   中英

克隆且可比的接口

[英]Cloneable and Comparable Interface

我的问题是关于Java中的复数。 我创建了一个类,成功地执行了一些数学运算,如加,减,乘和除。 但是我的问题是如何实现我不了解的可克隆和可比较的接口。 我了解克隆的概念,但我似乎无法执行它,因此具有可比性。 有什么想法吗? 谢谢。 您可以在下面查看我的代码。

 import java.util.Scanner; public class Complex implements Cloneable { private double real; private double imag; /*public Object clone() throws CloneNotSupportedException { Complex objClone = new Complex(); objClone.setReal(this.real); objClone.setImag(this.imag); return objClone; }*/ 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"; } 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()); } } 

可克隆的接口:

对象的克隆是具有不同标识和相同内容的对象。 要定义克隆,类必须实现可克隆的接口,并且必须使用公共修饰符覆盖Object的clone方法。 以下代码是克隆方法的简单替代。

public Complex clone() {
    // violation of contract to call super.clone() when creating instance of
    // the right class
    return new Complex(real,**img**);
}

可比接口:

可比较的接口将使对象能够判断给定的对象是小于还是大于或等于其自身。 当您对复杂对象数组进行排序时,这可能会很有用。 为了给对象一种能力,一个对象要实现可比较的接口并重写compareTo方法。 一个简单的例子是

public int compareTo(Complex other) {
   //do your comparison here and return appropriately 
   return this.getReal() - other.getReal(); 
}   

暂无
暂无

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

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