如何引用我在其上实现实例方法的对象。我写了一个叫MatrixMaker
类,看起来像这样:使用简单的打印方法打印数组对象
package one;
public class MatrixMaker {
private int rows;
private int columns;
public MatrixMaker(int m, int n){
rows = m;
columns = n;
double[][] matrix = new double[rows][columns];
}
public void printer(){
for(int i = 0; i < rows; i++){
for(int j = 0; j < columns; j++){
System.out.print(matrix[i][j]);
}
System.out.println();
}
}
}我使用初始化这个类的一个对象:
MatrixMaker matrix = new MatrixMaker(3,4);
我的问题是我怎么使用
matrix.printer();
打印阵列。我似乎无法参考方法printer()
中的对象内容。具体线路:
System.out.print(matrix[i][j]);
在你的类范围内定义'double [] [] matrix'。所以把它放在'private int columns;'的地方。 – 2014-11-02 20:53:47
谢谢!我甚至没有想到这一点。 – zyzz 2014-11-02 20:55:09