我正在编写一个程序来创建游戏板,并使用超类和子类来生成板。实质上,我想使用一个arrayList来存储从Cell的子类的方法返回的值。但我无法弄清楚如何去做。我在BoardGame类中写的东西给了我一个错误,我想不出另一种方式来做到这一点。 ArrayList中的存储方法返回值
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";
}
}
你得到什么错误到底是什么? – Vahx 2015-04-04 22:12:56
注意:'Cell'中的'landOn()'不会被任何其他'landOn()'方法覆盖(它是静态的)。 – 2015-04-04 23:27:35