2017-08-30 74 views
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()太代碼。

+0

無關:你想了解「清潔守則」(例如由羅伯特·馬丁偉大的書),並更具體地說:關於「單層抽象」原則。你的代碼可以從中受益。 – GhostCat

回答

2

如果要保留類型信息,泛型很有用。但泛型類型仍然需要有一些已知的接口才能使用它。由於doubleRational不共享通用接口,所以不可能直接創建適用於這兩種類型的通用實現。

你可以做的是創建一個接口Complex有2個實現,DoubleComplexRationalComplex

public interface Complex<T> { 

    T getReal(); 
    T getImaginary(); 

    Complex<T> opposite(Complex<T> z); 
    double abs(); 
    Complex<T> conjugate(); 
    Complex<T> inverse(); 

    Complex<T> add(Complex<T> z); 
    Complex<T> subtract(Complex<T> z); 
    Complex<T> product(Complex<T> z); 
    Complex<T> div(Complex<T> z); 

} 

public class DoubleComplex implements Complex<Double> { 

    private final double real; 
    private final double imaginary; 

    ... 

    @Override 
    public Complex<Double> add(Complex<Double> z) { 
     return new DoubleComplex(this.real + z.getReal(), this.imaginary + z.getImaginary()); 
    } 

    ... 
} 

public class RationalComplex implements Complex<Rational> { 

    private final Rational real; 
    private final Rational imaginary; 

    ... 

    @Override 
    public Complex<Rational> add(Complex<Rational> z) { 
     return new RationalComplex(this.real.add(z.getReal()), this.imaginary.add(z.getImaginary())); 
    } 

    ... 
}