我无法弄清楚如何从其测试文件中编写此代码。
public class MineNeighbourTest
{
private MineWorld world;
private Position position;
private MineNeighbour neighbour;
private MineNeighbour bigNeighbour;
/**
* Default constructor for the test class.
*/
public MineNeighbourTest()
{
}
/**
* Sets up the test fixture.
*
* Called before every test case method.
*/
@Before
public void setUp()
{
world = new MineWorld(6, 6);
position = new Position(world, 3, 1);
// create a field that has 4 mines in the neighbourhood
neighbour = new MineNeighbour(position, 4);
// create a field that has 8 mines in the neighbourhood
bigNeighbour = new MineNeighbour(position, 8);
}
/**
* Tears down the test fixture.
*
* Called after every test case method.
*/
@After
public void tearDown()
{
world = null;
position = null;
neighbour = null;
bigNeighbour = null;
}
/**
* Test of getNeighbouringMines method, of class MineNeighbour.
*/
@Test
public void testGetNeighbouringMines()
{
assertEquals(4, neighbour.getNumNeighbouringMines());
assertEquals(8, bigNeighbour.getNumNeighbouringMines());
}
/**
* Test of getStringRepresentation method, of class MineNeighbour.
*/
@Test
public void testGetStringRepresentation()
{
assertEquals("4", neighbour.getStringRepresentation());
assertEquals("8", bigNeighbour.getStringRepresentation());
}
}
这是我写的:
public class MineNeighbour extends Occupant
{
private Position pos;
public MineNeighbour(Position neighbour, Position bigNeighbour) {
super(neighbour);
}
public Position getNeighbouringMines() {
return neighbour;
return bigNeighbour;
}
@Override
public String getStringRepresenation() {
return getNeighbouringMines();
}
}
我需要使用getStringRepresentation()
和neighbour
的其他字段将bigNeighbour
覆盖为4& 8,我不太了解{{1}给我的信息}}。
我的乘员班:
MineNeighbourTest
}
答案 0 :(得分:1)
public class MineNeighbour extends Occupant
{
private int mines;
public MineNeighbour(Position neighbour, int mines) {
super(neighbour);
this.mines = mines;
}
public Position getNeighbouringMines() {
return mines;
}
@Override
public String getStringRepresenation() {
return Integer.toString(mines);
}
}