我想請問,我該怎麼initiallize最終場1和0,如果我想他們使用加法和乘法中性元素,如在環初始化複數JAVA
他們必須costantly
public class ComplexNumber {
/**Constant for multiplication. Value 1*/
//public final ComplexNumber ONE;
/**Constant for addition. Value 0 */
//public final ComplexNumber ZERO;
/**The real value of ComplexNumber! FINAL*/
private final double a;
/**The imaginary value of ComplexNumber! FINAL*/
private final double b;
/**
* This Constructor initializes real and imaginary values of ComplexNumber
* @param a double real value
* @param b double imaginary value
*/
ComplexNumber(double a, double b) {
this.a = a;
this.b = b;
//this.ONE = new ComplexNumber(1,0); //TODO ask
// this.ZERO = new ComplexNumber(0);
}
/**
* This Constructor initializes imaginary value of CompexNumber
* @param b double imaginary value
*/
ComplexNumber(double b){
this.a = 0;
this.b = b;
//this.ONE = new ComplexNumber(1,0); //TODO ask
// this.ZERO = new ComplexNumber(0);
}
謝謝!
謝謝!它的工作 –
但我不喜歡靜態在這種情況下,因爲如果我沒有任何對象,我不會需要常量!還是我不對? 但沒有靜態我有StackOverFlow異常 –
@DmitrySavkin如果你從不使用類,你將不會實例化常量。第一次使用該類時,靜態字段將被初始化。無論如何,這是一個常量,所以你應該聲明它是靜態的。當您在ComplexNumber的構造過程中實例化ComplexNumber時,StackOverflowException是正常的,因此您正在進行遞歸調用,該調用越來越深,沒有轉折點。 – Timmos