2016-04-21 30 views
1

我想請問,我該怎麼initiallize最終場1和0,如果我想他們使用加法和乘法中性元素,如在環初始化複數JAVA

他們必須costantly

enter image description here

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); 
} 

謝謝!

回答

6

如果你想從他們的常量,聲明並初始化它們是這樣的:

public static final ComplexNumber ONE = new ComplexNumber(1, 0); 
public static final ComplexNumber ZERO = new ComplexNumber(0, 0); 

static表示該字段是特定類的,而不是特定實例。 final表示該參考不能被重新分配。對於一個真正的常量,這個實例也必須是不可變的,我認爲你的代碼在ab之外不能從你的課程以外訪問。

+0

謝謝!它的工作 –

+0

但我不喜歡靜態在這種情況下,因爲如果我沒有任何對象,我不會需要常量!還是我不對? 但沒有靜態我有StackOverFlow異常 –

+0

@DmitrySavkin如果你從不使用類,你將不會實例化常量。第一次使用該類時,靜態字段將被初始化。無論如何,這是一個常量,所以你應該聲明它是靜態的。當您在ComplexNumber的構造過程中實例化ComplexNumber時,StackOverflowException是正常的,因此您正在進行遞歸調用,該調用越來越深,沒有轉折點。 – Timmos

0

constructor是初始化Java中的final變量的方法。

因爲最終只能初始化一次它的生命週期,你不能重新初始化它。

所以,使用參數調用構造函數是初始化的最佳方式。

+0

當我將它們初始化爲構造函數時,會出現StackOverFlowException! '代碼' 公共類ComplexNumber { \t/**恆爲乘法。值1 */ \t public final ComplexNumber ONE; \t/**添加的常量。值0 */ \t public static final ComplexNumber ZERO = new ComplexNumber(0,0); \t \t \t/** ComplexNumber的實際值! FINAL */ \t private final double a; \t \t \t/** ComplexNumber的虛數值! FINAL */ \t private final double b; \t ComplexNumber(double a,double b){ \t \t this.a = a; \t \t this.b = b; \t \t this.ONE = new ComplexNumber(1,0); \t \t \t \t} –

0
public class ComplexNumber { 
     final double realPart; 
     final double imgPart; 

     public static final ComplexNumber REAL_ONE = new ComplexNumber(1, 0); 
     public static final ComplexNumber ZERO = new ComplexNumber(0, 0); 

     public ComplexNumber(double realPart, double imgPart) { 
      this.realPart = realPart; 
      this.imgPart = imgPart; 
     } 

     public ComplexNumber(double imgPart) { 
      this(0, imgPart); 
     } 
    } 

爲了使您的代碼可讀,您可以將您的字段名稱重命名爲real和img部分。
並使用構造函數鏈,以便可以重用現有的構造函數。

編輯

public class ComplexNumber { 
     final double realPart; 
     final double imgPart; 

     public static final ComplexNumber REAL_ONE = new ComplexNumber(1, 0); 
     public static final ComplexNumber ZERO = new ComplexNumber(0, 0); 

     public ComplexNumber(double realPart, double imgPart) { 
      this.realPart = realPart; 
      this.imgPart = imgPart; 
     } 

     public static ComplexNumber createRealComplexNumber(double realPart) { 
      return new ComplexNumber(realPart, 0.0); 
     } 

     public static ComplexNumber createImgComplexNumber(double imgPart) { 
      return new ComplexNumber(0.0, imgPart); 
     } 
    } 

如果你想創建一個只實部的複數,你就不能過載構造函數,只有真正的一部分,因爲相同的簽名已被用過的。您可以使用此方法

+0

謝謝!它的工作原理 –

+0

使用@Timmos建議的方法初始化REAL_ONE&ZERO。在其上添加可讀性和構造器鏈接 –