我正在编写一个创建游戏板的程序,并使用超类和子类来生成游戏板。本质上,我想使用一个arrayList来存储从Cell子类的方法返回的值。但我无法弄清楚该怎么做。我在BoardGame类中写的内容给了我一个错误,我想不出另一种方法来做到这一点。 
import java.util.ArrayList;
public class BoardGame {
private Die theDie;
BoardGame(){
}
void buildBoard(){
ArrayList<Player> player;
Cell c = new Cell();
ArrayList<Cell>cells = new ArrayList<Cell>();
cells.add(c.landOn());
}
void runSimulation(){
}
int takeTurn(String n){
//since we don't know what we are returning yet
return 0;
}
String toString(String n){
//Since we aren't returning anything yet
return "Nothing";
}
}
public class Cell {
static int landOn(){
//Since we do not know what we are returning
return 0;
}
public String toString(){
//Since we do not know what we are returning
return "Blank";
}
}
public class Star extends Cell {
int landOn(){
//since we do not know what we are returning
return 5;
}
public String toString(){
//since we do not know what we are returning
return "Star: +5";
}
}
public class Lightning extends Cell {
int landOn(){
//since we do not know what we are returning
return -5;
}
public String toString(){
//since we do not know what we are returning
return "Lightning: -5";
}
}
public class X extends Cell {
int landOn(){
//since we do not know what we are returning
return -10;
}
public String toString(){
//since we do not know what we are returning
return "X: -10";
}
}
public class Smiley extends Cell {
int landOn(){
//since we do not know what we are returning
return 10;
}
public String toString(){
//since we do not know what we are returning
return "Smiley: +10";
}
}
答案 0 :(得分:1)
在代码中使用ArrayList<Cell>
将无效,因为c.landOn()
会返回int
。使用Integer
返回类型ArrayList<Integer>cells
将解决该特定问题。那是你看到的错误吗?
PS:还记得你可以说ArrayList<Cell>cells = new ArrayList<>();
而不是ArrayList<Cell>cells = new ArrayList<Cell>();
答案 1 :(得分:1)
您的ArrayList
被声明为Cell
方法的返回类型为int
。
您的ArrayList
无法存储除Cell
要存储所需方法的所有返回类型,需要ArrayList
类型的Integer
使用int
无法正常工作int
是原始的。
解决方案:
ArrayList<Integer> cells = new ArrayList<>();
答案 2 :(得分:0)
你可以这样做。
BoardGame课程:
public class BoardGame {
public BoardGame(){
//Get finalTreats from Cell class
int finalTreats = this.cell.finalTreats;
//Add finalTreats to cells ArrayList
cells.add(finalTreats);
}
//Get Cell class
Cell cell = new Cell();
//Initialize ArrayList
ArrayList<Integer> cells = new ArrayList<>();
}
细胞类:
public class Cell {
public int finalTreats;
public int landOn(int treats){
//Test for what it landed on based on treat amount
switch(treats){
//X
case -10: finalTreats = -10;
//Lightning
case -5: finalTreats = -5;
//Star
case 5: finalTreats = 5;
//Smiley face
case 10: finalTreats = 10;
//Blank
default: return 0;
}
}
}
星级(例如):
public class Star extends Cell{
public Star() {
this.landOn(5);
}
}
使其他类如Lighting和Smiley Face与Star类相同,但根据需要更改处理的值。
希望它有所帮助。
答案 3 :(得分:0)
您也可以这样做:
public abstract class Cell{
...
}
public class EmptyCell extends Cell{
...
}
public class Win5Treats extends Cell{
...
}
public class Lose5Treats extends Cell{
...
}
public class Win10Treats extends Cell{
...
}
public class Lose10Treats extends Cell{
...
}
public class Start extends Cell{
...
}
public class End extends Cell{
...
}
你的阵列就是这样的:
ArrayList<Cell> cells = new ArrayList<Cell>();