2016-11-08 65 views
-2

我想創建一個類,它將通過以一個數組作爲輸入(其中包括)創建一個矩陣。這個數組將被分配給一個記錄(Record9)。然而,我在編譯時遇到這個錯誤。您可以在下面找到我的代碼:作業:錯誤:無效的方法聲明;返回類型要求

public class Matrix3x3flat { 

    private class Record9 { 
     public long r1c1; 
     public long r1c2; 
     public long r1c3; 

     public long r2c1; 
     public long r2c2; 
     public long r2c3; 

     public long r3c1; 
     public long r3c2; 
     public long r3c3; 
    } 
    private Record9 mat; 

    public Record9(long[] arr) { 
     Record9 this.mat = new Record9(); 

     this.mat.r1c1 = arr[0]; 
     this.mat.r1c2 = arr[1]; 
     this.mat.r1c3 = arr[2]; 
     this.mat.r2c1 = arr[3]; 
     this.mat.r2c2 = arr[4]; 
     this.mat.r2c3 = arr[5]; 
     this.mat.r3c1 = arr[6]; 
     this.mat.r3c2 = arr[7]; 
     this.mat.r3c3 = arr[8]; 

     return this.mat; 
    }  
} 

我不明白的問題,但我懷疑它是與我在return語句不正確引用this.mat。

回答

0

好吧,我注意到一些事情。編輯:如下所述,構造函數名稱需要與類名稱相同。

2)爲什麼你重新聲明墊作爲Record9類型。你已經將它設置爲Record9的一種類型,不需要再次定義它,你可以說this.mat =無論它需要是什麼

0

我的想法是你想在公共Record9上創建Record9的實例( long [] arr),目前你在構造函數中使用return語句,它是不允許的。所以你需要將其轉換爲方法。

嘗試這樣的:

公共類Matrix3x3flat {

private class Record9 { 
    public long r1c1; 
    public long r1c2; 
    public long r1c3; 

    public long r2c1; 
    public long r2c2; 
    public long r2c3; 

    public long r3c1; 
    public long r3c2; 
    public long r3c3; 
} 
private Record9 mat; 

public Record9 instance(long[] arr) { 
    this.mat = new Record9(); 

    this.mat.r1c1 = arr[0]; 
    this.mat.r1c2 = arr[1]; 
    this.mat.r1c3 = arr[2]; 
    this.mat.r2c1 = arr[3]; 
    this.mat.r2c2 = arr[4]; 
    this.mat.r2c3 = arr[5]; 
    this.mat.r3c1 = arr[6]; 
    this.mat.r3c2 = arr[7]; 
    this.mat.r3c3 = arr[8]; 

    return this.mat; 
}  

}

相關問題