Java 作业:错误:方法声明无效;需要返回类型
我正在尝试创建一个类,该类将通过将数组作为输入(以及其他内容)来创建矩阵。此数组将被分配给一个记录(Record9)。然而,我在尝试编译时遇到了这个错误。您可以在下面找到我的代码:Java 作业:错误:方法声明无效;需要返回类型,java,oop,constructor,Java,Oop,Constructor,我正在尝试创建一个类,该类将通过将数组作为输入(以及其他内容)来创建矩阵。此数组将被分配给一个记录(Record9)。然而,我在尝试编译时遇到了这个错误。您可以在下面找到我的代码: public class Matrix3x3flat { private class Record9 { public long r1c1; public long r1c2; public long r1c3; public long r2
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有关。好吧,我注意到的事情很少。编辑:如下所述,构造函数名称必须与类名相同
2) 为什么要将mat声明为记录类型9。您已经将它设置为Record9的一种类型,无需再次定义它,您可以这样说。mat=无论它需要什么,我的想法是您想要在公共Record9(long[]arr)上创建Record9的实例,目前您使用的是构造函数的返回语句i端,这是不允许的。所以你需要把它转换成一个方法 试着这样做: 公屋{
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;
}
}