如何引用我在其上實現實例方法的對象。我寫了一個叫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