【发布时间】:2015-08-20 18:07:05
【问题描述】:
我有一个带有方法 setMapName 的 Map 类,它从 Enum mapName 中选择来设置它,在构造函数内部有一个规则,可以根据 mapName 值从数组扇区中命名一个单元格 Alien 或 Human,我想测试单元格的名称是否确实是 Alien 或 Human,但我得到 null。
public class Map {
private Name mapName;
private final Sector [][] sector;
private int Matrix [][];
private static final int X=23;
private static final int Y=14;
public Map (){
sector = new Sector[X][Y];
for (int i=0; i < X; i++){
for (int j=0; j<Y; j++) {
sector[i][j] = new Sector (i,j);
}
}
Matrix = new int[23][14];
if(mapName==Name.FERMI){
sector[10][8]=new Alien(10,8);
sector[10][9]=new Human(10,9);
}
if(mapName==Name.GALILEI||mapName==Name.GALVANI){
sector[10][5]=new Alien(10,5);
sector[10][7]=new Human(10,7);
}
}
public int[][] getMatrix() {
return Matrix;
}
public void setMatrix(int matrix[][]) {
Matrix = matrix;
}
public Name getMapName() {
return mapName;
}
public void setMapName(Name mapName) {//this is the method i want to use before the constructor
this.mapName = mapName;
}
public Sector[][] getSectors(){
return sector;
}
public void addSectors(){
Sector.add(sector);
}
}
public enum Name {
FERMI, GALILEI, GALVANI
}
public class MapTest {
@Test
public void testMapAlien(){
Map map = new Map();
map.setMapName(Name.FERMI);
assertEquals(Name.Alien, map.getSectors()[10][8].getSectorName());
}
}
【问题讨论】:
标签: java methods constructor call