2016-11-08 68 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; 
}  

}

相关问题