我的问题是关于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());
}
}
答案 0 :(得分:0)
可克隆界面:
对象的克隆是具有不同身份和相同内容的对象。要定义clone,类必须实现可克隆接口,并且必须使用public修饰符覆盖Object的clone方法。以下代码是克隆方法的简单覆盖。
public Complex clone() {
// violation of contract to call super.clone() when creating instance of
// the right class
return new Complex(real,**img**);
}
可比较的界面:
类似的界面将使对象能够判断给定对象是否会更少或更大或等于自身。在对复杂对象进行排序时,它可能很有用。为了赋予对象这个能力a,可以实现类似的接口并覆盖compareTo方法。一个简单的例子是
public int compareTo(Complex other) {
//do your comparison here and return appropriately
return this.getReal() - other.getReal();
}