0

我有一个 ArrayList 的 Tiles 有两个值;一个整数和一个字符串。

我需要获取这两个值,所以在编写代码时,我决定确保它正常工作:

private void setIcons()
    {
        ArrayList<Tiles> tiles = new ArrayList<>();
        tiles = game.getTiles(0);
        Tiles i = tiles.get(0);
        String s = i.returnTileName();
        System.out.println(i.returnTileName() +" ," +i.returnTileNumber());
    }

这是我的 Tiles 类,它有子类:

public class Tiles
{
    int number;
    String name;

    public Tiles()
    {

    }

    public String returnTileName()
    {
        return name;
    }

    public int returnTileNumber()
    {
        return number;
    }
}

'Tiles' 有几个子类,它们的结构几乎相同:

public class Bamboo extends Tiles
{
    int number;
    String name;
    public Bamboo(int number, String name)
    {
        this.number = number;
        this.name = name;
    }
}

当我运行它时,字符串返回为“null”,而我的 int 返回为“0”,当它们明显具有值时,如下图所示。

http://img716.imageshack.us/img716/6154/emx4.jpg

我在 setIcons() 中调用的方法:

 public ArrayList getTiles(int playerX)
    {
        ArrayList<Tiles> tiles = new ArrayList<>();
        Player pl = player.get(playerX);
        tiles = pl.playerTiles;
        return tiles;
    }

“Game”类中有两个 ArrayList,“tilesDeck”是 Tiles 的 ArrayList,“player”是 Player 类的 ArrayList。

这是我的游戏课。以前不知道它有滚动功能。

public class Game
{
    ArrayList<Tiles> tilesDeck = new ArrayList<>();
    ArrayList<Player> player = new ArrayList<>();

    public Game()
    {
        addTile("Bamboo", 1);
        addTile("Bamboo", 2);
        addTile("Bamboo", 3);
        addTile("Bamboo", 4);
        addTile("Bamboo", 5);
        addTile("Bamboo", 6);
        addTile("Bamboo", 7);
        addTile("Bamboo", 8);
        addTile("Bamboo", 9);

        addTile("Circles", 1);
        addTile("Circles", 2);
        addTile("Circles", 3);
        addTile("Circles", 4);
        addTile("Circles", 5);
        addTile("Circles", 6);
        addTile("Circles", 7);
        addTile("Circles", 8);
        addTile("Circles", 9);

        addTile("Characters", 1);
        addTile("Characters", 2);
        addTile("Characters", 3);
        addTile("Characters", 4);
        addTile("Characters", 5);
        addTile("Characters", 6);
        addTile("Characters", 7);
        addTile("Characters", 8);
        addTile("Characters", 9);

        addTile("Dragons", 1);
        addTile("Winds", 1);

        Collections.shuffle(tilesDeck);
        player.add(new Player("Player 1"));
        player.add(new Player("Player 2"));
        player.add(new Player("Player 3"));
        player.add(new Player("Player 4"));

        startTiles();
    }

    private void addTile(String name, int number)
    {
        int counter = 0;
        if (name.contains("Bamboo"))
        {
            while (counter < 4)
            {
                tilesDeck.add(new Bamboo(number, name));
                counter++;
            }
        }

        if (name.contains("Circles"))
        {
             while (counter < 4)
            {
                tilesDeck.add(new Circles(number, name));
                counter++;
            }
        }

        if (name.contains("Characters"))
        {
             while (counter < 4)
            {
                tilesDeck.add(new Characters(number, name));
                counter++;
            }
        }

        if (name.contains("Winds"))
        {
             while (counter < 4)
            {
                tilesDeck.add(new Winds("East"));
                tilesDeck.add(new Winds("South"));
                tilesDeck.add(new Winds("West"));
                tilesDeck.add(new Winds("North"));
                counter++;
            }
        }

        if (name.contains("Dragons"))
        {
             while (counter < 4)
            {
                tilesDeck.add(new Dragons("Red"));
                tilesDeck.add(new Dragons("Green"));
                tilesDeck.add(new Dragons("White"));
                counter++;
            }
        }
    }

    private void startTiles()
    {
        int playerCounter = 0;

        while (playerCounter < 4)
        {
            Player pl = player.get(playerCounter);
            int counter = 0;
            while (counter <13)
            {
                pl.playerTiles.add(tilesDeck.get(0));
                tilesDeck.remove(0);
                counter++;
            }
            playerCounter++;
        }
    }

    public ArrayList getTiles(int playerX)
    {
        ArrayList<Tiles> tiles = new ArrayList<>();
        Player pl = player.get(playerX);
        tiles = pl.playerTiles;
        return tiles;
    }
}
4

4 回答 4

1

问题是您的子类正在创建重复的数字和名称变量:

public class Bamboo extends Tiles
{
    int number; // <-- remove this
    String name; // <-- and this
    public Bamboo(int number, String name)
    {
        this.number = number;
        this.name = name;
    }
}

Bamboo 类正在初始化重复的 number 和 name 变量,但returnTileName()andreturnTileNumber()方法正在从 Tiles 超类返回未初始化的变量。

于 2013-10-17T08:34:49.140 回答
0
public Tiles()
{

}

在那里初始化你的变量。

于 2013-10-17T08:18:50.730 回答
0

使用一些默认值初始化变量以避免获得空值。

public class Tiles
{
// assign your default values
int number = 1;           
String name = "default name";

public Tiles()
{

}

public String returnTileName()
{
    return name;
}

public int returnTileNumber()
{
    return number;
}
}
于 2013-10-17T08:19:36.190 回答
0

您必须在构造函数中初始化变量

于 2013-10-17T08:22:08.720 回答