2015-04-04 111 views
0

我正在编写一个程序来创建游戏板,并使用超类和子类来生成板。实质上,我想使用一个arrayList来存储从Cell的子类的方法返回的值。但我无法弄清楚如何去做。我在BoardGame类中写的东西给了我一个错误,我想不出另一种方式来做到这一点。 GameBoardArrayList中的存储方法返回值

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

你得到什么错误到底是什么? – Vahx 2015-04-04 22:12:56

+0

注意:'Cell'中的'landOn()'不会被任何其他'landOn()'方法覆盖(它是静态的)。 – 2015-04-04 23:27:35

回答

1

ArrayList声明为Cell 为方法的返回类型为int。 您ArrayList不能存储任何其他则实例类型的Cell

来存储所有的返回类型的,你需要的类型Integer

使用int不会工作,无论是作为intArrayList方法是一种原始的。

解决方案:

ArrayList<Integer> cells = new ArrayList<>(); 
1

使用ArrayList<Cell>如代码将无法正常工作,为c.landOn()返回int。使用Integer返回类型ArrayList<Integer>cells将解决该特定问题。那是你看到的错误吗?

PS:还记得你就可以说的ArrayList<Cell>cells = new ArrayList<>();代替ArrayList<Cell>cells = new ArrayList<Cell>();

0

你可以做这样的事情。

棋盘游戏类:

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<>(); 
} 

Cell类:

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); 
    } 
} 

使其它类,如照明和笑脸一样的星级,但根据需要改变对待的价值。

希望它有帮助。

+0

'case'语句看起来是一个糟糕的主意 - 每个'Cell'应该返回一个调整量(这正是我认为它首先要做的),或者将它当作委托来处理,以便单元执行调整。事实上,你最终会在两个不同的地方制定规则。 – 2015-04-04 23:26:22

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>();