public class Tetromino {
Cell[] cells;
public Tetromino(){
cells = new Cell[4];
}
}
public class T extends Tetromino{
Cell[] cells;//如果在这里写了这一句代码,就会报NullPointerException
public T(){
this(0,0);
}
public T(int a,int b){
this.cells=new Cell[]{new Cell(a,b),new Cell(a,b+1),new Cell(a-1,b+1),new Cell(a,b+2)};
}
}
public class Test {
public static void print(Tetromino te){
Cell[] cells=te.cells;
boolean mark=false;
for(int i=0;i<=19;i++){
for(int j=0;j<=9;j++){
for(int k=0;k<4;k++){
if((i==cells[k].row)&(j==cells[k].col)){
System.out.print("*");
mark =true;
break;
}
else{
mark =false;
}
}
if(mark==false){
System.out.print("-");
}
}
System.out.println();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Tetromino t = new T(1,1);
print(t);
}
}上面这段代码,如果T类中不写
Cell[] cells;
就不会报错,但是写了之后,在26行如下代码行就会提示异常,请问是什么原因呢??在线等,谢谢。
if((i==cells[k].row)&(j==cells[k].col)){
shenzhi
慕侠7578997
随时随地看视频慕课网APP
相关分类