类文件:我使用2d int数组来存储值然后我设置了setter和getter
public class SquareMatrix implements SquareMatrixInterface{
int[][] squareMatrix;
public int[][] getSquareMatrix() {
return squareMatrix;
}
public void setSquareMatrix(int[][] squareMatrix) {
this.squareMatrix = squareMatrix;
}
}
接口文件
public interface SquareMatrixInterface {
public int[][] getSquareMatrix();
public void setSquareMatrix(int[][] squareMatrix);
}
Main:我输入一个值并尝试将值设置为setter但它给出了错误
public class test {
SquareMatrixInterface matrixA = new SquareMatrix();
SquareMatrixInterface matrixB = new SquareMatrix();
public static void main(String[] args) {
int[][] m1 = {{1,2},{3,4}};
matrixA.setSquareMatrix(m1);
}
}
为什么当我尝试将数组传递给setter时它会给我错误 如何在没有包含java库的情况下传递二维数组
答案 0 :(得分:3)
您无法访问静态块内的非静态属性.. [无法对非静态字段进行静态引用]因此您需要将main方法重写为..
public static void main(String[] args) {
SquareMatrixInterface matrixA = new SquareMatrix();
int[][] m1 = {{1, 2}, {3, 4}};
matrixA.setSquareMatrix(m1);
}
或强>
static SquareMatrixInterface matrixA = new SquareMatrix();
public static void main(String[] args) {
int[][] m1 = {{1, 2}, {3, 4}};
matrixA.setSquareMatrix(m1);
}