Java 如何重写字符串
我不知道如何从测试文件中编写这段代码Java 如何重写字符串,java,Java,我不知道如何从测试文件中编写这段代码 public class MineNeighbourTest { private MineWorld world; private Position position; private MineNeighbour neighbour; private MineNeighbour bigNeighbour; /** * Default constructor for the test class.
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()
改写为4和8,添加Neighbourt
和BigNeighbourt
字段,我不太理解MineNeighbourTest
提供给我的信息
我的乘客阶级:
public class Occupant
{
private Position pos;
public Occupant(Position iniPos) {
this.pos = iniPos;
}
public Position getPosition() {
return pos;
}
public void setPosition(Position newPos) {
this.pos = newPos;
}
public String getStringRepresentation() {
return " ";
}
}这段代码甚至可以编译吗?您有一个方法有两个返回,这是不可能的。你能澄清你的问题吗?至少对我来说,这是令人困惑的。我不理解这一点,你的
GetNeighborRouringMines()
方法有两个紧接着的返回语句?不,我不确定我需要如何声明我的方法并重写GetStringRepresentation。我认为测试文件要求我做的是创建两个额外的字段Nexter和BigNexter。我应该使用MineNeighbourTest来完成我在MineNeighbor类中的代码,但我不太理解测试文件。在不知道占用者做什么的情况下,super(Neighbor)
对我来说没有多大意义。而且,我认为在你尝试更多的测试之前,你需要先让你的邻居工作起来。它看起来不像你期望的那样。
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);
}
}