0
我有一個類用複數,實部和虛部作爲雙精度來計算。 在其他部分我有一個合理的類來計算有理數。 現在我想讓我的複合類可以在實部和虛部都是有理數的情況下運行。我已經閱讀了一些關於泛型的文檔,但我不知道如何將真實部分和虛部作爲泛型進行聲明,並且在真實和虛擬部分是雙精度或有理數時使用方法加2個複數。 這是我的測試代碼:用Java中的複雜類聲明泛型變量
import java.util.regex.Pattern;
public class Complex {
private double real;
private double imaginary;
private Rational qreal;
private Rational qimaginary;
public Complex(double real, double imaginary) {
super();
this.real = real;
this.imaginary = imaginary;
}
public Complex(Rational real, Rational imaginary) {
this.qreal = real;
this.qimaginary = imaginary;
}
public Complex(String z) {
z = z.replaceAll(" ","");
if(z.contains("i") || z.contains("j")){
if(z.contains("+")) {
String[] z1 = z.split(Pattern.quote("+"));
this.real = Double.parseDouble(z1[0]);
this.imaginary = Double.parseDouble(z1[1].substring(0, z1.length-1));
}
else if(z.contains("-")) {
String[] z1 = z.split(Pattern.quote("-"));
this.real = Double.parseDouble(z1[0]);
this.imaginary = -Double.parseDouble(z1[1].substring(0, z1.length-1));
}
else System.out.println("Syntax Error");
}
else System.out.println("The complex must only contains i or j as imaginary unit");
}
public double getReal() {
return real;
}
public void setReal(double real) {
this.real = real;
}
public double getImaginary() {
return imaginary;
}
public Rational getQreal() {
return qreal;
}
public void setQreal(Rational qreal) {
this.qreal = qreal;
}
public Rational getQimaginary() {
return qimaginary;
}
public void setQimaginary(Rational qimaginary) {
this.qimaginary = qimaginary;
}
public void setImaginary(double imaginary) {
this.imaginary = imaginary;
}
Complex opposite(Complex z) {return new Complex(-z.real, -z.imaginary);}
double abs() {return Math.hypot(this.real, this.imaginary);}
Complex conjugate() {return new Complex(real, -imaginary);}
Complex inverse() {
if(this.real == 0 && this.imaginary == 0) return new Complex(Double.NaN, Double.NaN);
else {
Complex c = this.conjugate();
double abs_square = Math.pow(this.abs(), 2.);
return new Complex(c.real/abs_square, c.imaginary/abs_square);
}
}
Complex add2(Complex z) {
System.out.println("Suma " + this.qreal.add(z.qreal) + " " + this.qimaginary.add(z.qimaginary) + "i");
return new Complex(this.qreal.add(z.qreal), this.qimaginary.add(z.qimaginary));
}
Complex add(Complex z) {return new Complex(this.real + z.real, this.imaginary + z.imaginary);}
Complex subtract(Complex z) {return add(z.opposite(z));}
Complex product(Complex z) {
double r, i;
r = this.real * z.real - this.imaginary * z.imaginary;
i = this.real * z.imaginary + this.imaginary * z.real;
return new Complex(r, i);
}
Complex div(Complex z) {
Complex num = this.product(z.conjugate());
double den = Math.pow(Math.hypot(z.real, z.imaginary), 2.);
return new Complex(num.real/den, num.imaginary/den);
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Complex [real=" + real + ", imaginary=" + imaginary + ", qreal=" + qreal + ", qimaginary=" + qimaginary
+ "]";
}
/*@Override
public String toString() {
if(imaginary > 0.) {
if (imaginary == 1.)
return real + " + " + "i";
return real + " + " + imaginary + "i";
}
else if(imaginary < 0.) {
if (imaginary == -1.)
return real + " - " + "i";
return real + " " + imaginary + "i";
}
else if(imaginary == 0.)
return "" + real;
else if(real == 0.)
return imaginary + "i";
else
return "0";
}*/
}
如果你看到我實現了2種添加方法,但我想只有一個,因此,對於其他方法,的toString()太代碼。
無關:你想了解「清潔守則」(例如由羅伯特·馬丁偉大的書),並更具體地說:關於「單層抽象」原則。你的代碼可以從中受益。 – GhostCat