@Hubertoo
2017-07-17T05:20:43.000000Z
字数 4613
阅读 773
达内
this.row = row;new Cell()就是在初始化成员变量
public Cell(int row, int col){
this.row = row;
this.col = col;
}
public Cell(int n){
this(n,n)
}
public Cell(){
this(0,0);
}
Cell[] cells = new Cell[4];
Cell[] cells = new Cell[4];cells[0] = new Cell(1,3);cells[1] = new Cell(2);cells[2] = new Cell();cells[3] = new Cell(1,2);实际在堆中开始的cells[i]也是存放的地址信息,还是要用构造方法来初始化指向的对象
int[][] arr = new int[3][];
arr[0] = new int[2];
arr[1] = new int[3];
arr[2] = new int[2];
arr[][] 里面都初始化为0
这种问题可以用栈和堆画图来形象表示,这种类型可以表示类似“矩阵”这样的数据结构
package day02;public class Cell {int row;int col;//下落一行public void drop(){row++;}//drop方法的重载public void drop(int d){row += d;}//左移一列public void moveLeft(){col --;}//重载moveLeft()public void moveLeft(int d){col -= d;}//右移一列public void moveRight(){col++;}//获取格子信息public String getCellInfo(){return row + ",,," + col;}}
//添加构造方法以及重载public Cell(int row, int col){this.row = row;this.col = col;}public Cell(int d){this(d,d);}public Cell(){this(0,0);}
T类
package day02;public class T {Cell[] cells;//构造方法用于T类的初始化public T(int row, int col){cells = new Cell[4]; //顺时针初始化cells[0] = new Cell(row, col);cells[1] = new Cell(row,col++);cells[1] = new Cell(row, col+2);cells[0] = new Cell(row+1, col+1);}public T(){this(0, 0);}//打印格子中四个格子所在的坐标public void print(){String str = "";for(int i=0; i<cells.length-1; i++){str +="(" +cells[cells.length-1].getCellInfo() + ")";System.out.println(str);}System.out.println(str);}//方块的下落一个格子public void drop(){for(int i=0; i<cells.length;i++){cells[i].row++;}}//方块左移一个格子public void moveLeft(){for(int i=0; i<cells.length;i++){cells[i].col--;}}//方块右移一个格子public void moveRight(){for(int i=0; i<cells.length;i++){cells[i].col++;}}}
T测试类
package day02;public class TestT {public static void main(String[] args) {T t = new T(0, 1);//测试print方法System.out.println("原始坐标为:");t.print();// //测试drop();// t.drop();// System.out.println("调用drop方法后的坐标:");// t.print();//// //测试moveLeft()// t.moveLeft();// System.out.println("调用moveLeft()后的坐标:");// t.print();//// //测试moveRight()// t.moveRight();// System.out.println("调用moveRight()后的坐标:");// t.print();}}
J类
package day02;public class J {Cell[] cells;//构造方法用于T类的初始化public J(int row, int col){cells = new Cell[4]; //顺时针初始化cells[0] = new Cell(row, col);cells[1] = new Cell(row, col+1);cells[2] = new Cell(row, col+2);cells[3] = new Cell(row+1, col+2);}public J(){this(0, 0);}//打印格子中四个格子所在的坐标public void print(){String str = "";for(int i=0; i<cells.length-1; i++){str += "(" + cells[i].getCellInfo() + "),";}str +="(" +cells[cells.length-1].getCellInfo() + ")";System.out.println(str);}//方块的下落一个格子public void drop(){for(int i=0; i<cells.length;i++){cells[i].row++;}}//方块左移一个格子public void moveLeft(){for(int i=0; i<cells.length;i++){cells[i].col--;}}//方块右移一个格子public void moveRight(){for(int i=0; i<cells.length;i++){cells[i].col++;}}}
J测试类
package day02;public class TestJ {public static void main(String[] args) {J j = new J(0, 1);//测试print方法System.out.println("原始坐标为:");j.print();// //测试drop();// t.drop();// System.out.println("调用drop方法后的坐标:");// t.print();//// //测试moveLeft()// t.moveLeft();// System.out.println("调用moveLeft()后的坐标:");// t.print();//// //测试moveRight()// t.moveRight();// System.out.println("调用moveRight()后的坐标:");// t.print();}}
package day02;public class O {Cell[] cells;public O(int row, int col){cells = new Cell[4];cells[0] = new Cell(row, col);cells[1] = new Cell(row, col+1);cells[2] = new Cell(row+1, col);cells[3] = new Cell(row+1, col+1);}public O(){this(0,0);}public void print(){String str = "";for(int i=0; i<cells.length-1; i++){str += "(" + cells[i].getCellInfo() + "),";}str +="(" +cells[cells.length-1].getCellInfo() + ")";System.out.println(str);}//方块的下落一个格子public void drop(){for(int i=0; i<cells.length;i++){cells[i].row++;}}//方块左移一个格子public void moveLeft(){for(int i=0; i<cells.length;i++){cells[i].col--;}}//方块右移一个格子public void moveRight(){for(int i=0; i<cells.length;i++){cells[i].col++;}}}
TestT类
package day02;public class TestO {public static void main(String[] args) {O o = new O(0, 1);//测试print方法System.out.println("原始坐标为:");o.print();// //测试drop();// o.drop();// System.out.println("调用drop方法后的坐标:");// o.print();//// //测试moveLeft()// o.moveLeft();// System.out.println("调用moveLeft()后的坐标:");// o.print();//// //测试moveRight()// o.moveRight();// System.out.println("调用moveRight()后的坐标:");// o.print();}}