2017-06-05 97 views
-2

我有關於構造函數和參數的具體問題。使用字節數組作爲構造函數的參數:編譯器錯誤,由於未找到參數

我使用byte []數組作爲我的對象構造函數的參數。我的對象類編譯得很好,但測試類有一個錯誤,我不明白。 我在測試類中使用正確的參數,但編譯器說構造函數中沒有任何參數。只是我沒有看到這個缺陷,但我對這一個很有吸引力。

預先感謝您!

編譯器錯誤:

CubeTest.java:6: error: constructor Cube in class Cube cannot be applied to given types; 
    Cube test = new Cube(); 
       ^
required: byte[],byte[],byte[],byte[],byte[],byte[] 
found: no arguments 
reason: actual and formal argument lists differ in length 
1 error 

魔方類:

class Cube{ 
//declare arrays 
private byte[] f; 
private byte[] r; 
private byte[] u; 
private byte[] b; 
private byte[] l; 
private byte[] d; 
// 
// 
// 
//constructor 
public Cube(byte[] f, byte[] r, byte[] u, byte[] b, byte[] l, byte[] d){ 


    this.f = new byte[9]; 
    this.r = new byte[9]; 
    this.u = new byte[9]; 
    this.b = new byte[9]; 
    this.l = new byte[9]; 
    this.d = new byte[9]; 


    //fill this.f 
    for(int i = 0, n = 0; i < 9 && n < 9; i++, n++){ 
     this.f[i] = f[n]; 
    } 
    //fill this.r 
    for(int i = 0, n = 0; i < 9 && n < 9; i++, n++){ 
     this.r[i] = r[n]; 
    } 
    //fill this.u 
    for(int i = 0, n = 0; i < 9 && n < 9; i++, n++){ 
     this.u[i] = u[n]; 
    } 
    //fill this.b 
    for(int i = 0, n = 0; i < 9 && n < 9; i++, n++){ 
     this.b[i] = b[n]; 
    } 
    //fill this.l 
    for(int i = 0, n = 0; i < 9 && n < 9; i++, n++){ 
     this.l[i] = l[n]; 
    } 
    //fill this.d 
    for(int i = 0, n = 0; i < 9 && n < 9; i++, n++){ 
     this.d[i] = d[n]; 
    } 
} 
} 

測試類:

class CubeTest{ 
public static void main(String args[]){ 
    //f, r, u, b, l, d 
    //1, 2, 3, 4, 5, 6 
    //create such arrays 
    byte[] f = new byte[9]; 
    byte[] r = new byte[9]; 
    byte[] u = new byte[9]; 
    byte[] b = new byte[9]; 
    byte[] l = new byte[9]; 
    byte[] d = new byte[9]; 
    //fill arrays 
    for(int i = 0; i < 9; i++){ 
     f[i] = 1; 
     r[i] = 2; 
     u[i] = 3; 
     b[i] = 4; 
     l[i] = 5; 
     d[i] = 6; 
    } 
    //create Cube named test 
    Cube test = new Cube(f, r, u, b, l, d); 
    // 
} 
} 
+1

錯誤來自以下語句:'Cube test = new Cube();'但是你不在顯示的代碼中這樣做。檢查你的實際代碼並重新編譯它,它應該工作,因爲你的構造函數調用遵守構造函數中指定的參數。 – davidxxx

+0

@davidxxx這是我的問題:我不這樣做在我的代碼中,我甚至沒有在第6行,但編譯器告訴我:/ –

+0

嗯。你可能有些東西你看不到:嘗試用另一個文本編輯器打開你的CubeTest類並檢查它的內容。然後確保你編譯這個類而不是它的副本。 – davidxxx

回答

0

你必須在你的類立方提供一個默認的構造函數了。 當您聲明一個自定義構造函數時,Java編譯器不會創建一個默認構造函數。

+0

但問題是這個可疑的編譯器錯誤:我甚至不使用默認的構造函數,但編譯器告訴我(這是我的意見錯誤) –

+0

如果添加默認構造函數會發生什麼?你仍然得到這個錯誤? –