Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java通过数组访问对象_Java_Arrays - Fatal编程技术网

Java通过数组访问对象

Java通过数组访问对象,java,arrays,Java,Arrays,我试图用Java制作垄断/欧洲商业游戏,但我遇到了一个简单的问题。。。 所以,我有基本的字段类 public class Field { protected int fieldPosition; Field(){ } } 然后我将这个类扩展到Start、Property、Jail、Chance等子类 目前我正在从事财产类的工作 public class Property extends Field{ private String propertyGroup;

我试图用Java制作垄断/欧洲商业游戏,但我遇到了一个简单的问题。。。 所以,我有基本的字段类

public class Field {
    protected int fieldPosition;

    Field(){    }
}
然后我将这个类扩展到Start、Property、Jail、Chance等子类

目前我正在从事财产类的工作

public class Property extends Field{
    private String propertyGroup;
    private String propertyOwner;
    private String propertyName;
    private float propertyPrice;
    private boolean allowBuilding;
    private int numberOfHouses;
    private int numberOfHotels;
    private float basicValue;
    private float houseValue;

    final private int maxNumberOfHouses = 4;
    final private int getMaxNumberOfHotels = 1;

    public Property(int fieldPosition, String propertyGroup, String propertyName, float propertyPrice, float basicValue, float houseValue){
        this.fieldPosition = fieldPosition;
        this.propertyGroup = propertyGroup;
        propertyOwner = "none";
        this.propertyName = propertyName;
        this.propertyPrice = propertyPrice;
        allowBuilding = false;
        this.basicValue = basicValue;
        this.houseValue = houseValue;
        numberOfHotels = 0;
        numberOfHotels = 0;
    }

    /*
    Setters
     */
    public void setAllowBuilding(boolean allowBuilding) {
        this.allowBuilding = allowBuilding;
    }

    public void setPropertyOwner(String propertyOwner) {
        this.propertyOwner = propertyOwner;
    }

    /*
    Getters
     */

    public String getPropertyGroup() { return propertyGroup; }

    public String getPropertyOwner() {
        return propertyOwner;
    }

    public String getPropertyName() {
        return propertyName;
    }

    public float getPropertyPrice() {
        return propertyPrice;
    }

    public boolean isAllowBuilding() {
        return allowBuilding;
    }

    public int getNumberOfHouses() {
        return numberOfHouses;
    }

    public int getNumberOfHotels() {
        return numberOfHotels;
    }

    public int getMaxNumberOfHouses() {
        return maxNumberOfHouses;
    }

    public float getBasicValue() {
        return basicValue;
    }

    public float getHouseValue() {
        return houseValue;
    }



    /*
         Add and remove house and hotel methods
         */
    public void addHouse(){
            this.numberOfHouses = getNumberOfHouses() + 1;
            this.propertyPrice = getPropertyPrice() + 100;
    }

    public void removeHouse(){
            this.numberOfHouses = getNumberOfHouses() - 1;
            this.propertyPrice = getPropertyPrice() - 100;
    }

    public void addHotel(){
            this.numberOfHotels = getNumberOfHotels() + 1;
            this.propertyPrice = getPropertyPrice() + 400;
    }

    public void removeHotel(){
            this.numberOfHotels = getNumberOfHotels() - 1;
            this.propertyPrice = getPropertyPrice() - 400;
    }

    @Override
    public String toString() {
        return propertyName + getNumberOfHouses();
    }
下一个类是Board,它应该创建所有字段的基本表(使用polymorphysm,其中一些是属性,一些是其他的)

然后在游戏类中,我得到了方法buyProperty

  public void buyProperty(Player player, Property property){
    if(player.getPlayerCash() >= property.getPropertyPrice() &&
            (property.getPropertyOwner().equals("none"))){
        player.addProperty(property);
        property.setPropertyOwner(player.getPlayerName());
        player.removeCash(property.getPropertyPrice());
    }
}
最后,我试图测试它,但不幸的是,它不起作用

public static void main(String[] args) {
    Board board = new Board();
    Game game = new Game();
    Player johny = new Player("Johny");

    game.buyProperty(johny, board[1]);

}
java:需要数组,但找到game.components.Board

它指向了我贴的最后一行


有什么想法和建议吗?

这是一个
类,包含
现场板[]

Board board = new Board();
因此,是的,下面的行不起作用,因为
board
board
类,而不是
board
类中的
属性[]数组

game.buyProperty(johny, board[1]);
我想你想要达到的是

game.buyProperty(johny, board.getBoard()[1]);
编辑:感谢秋葵的更正。

您应该考虑移动<代码>字段板[]/COD>从构造函数到板类,然后提供一个公共方法来访问<代码>字段板[]/COD>:

public class Board {
    // Move board  
    Field board[] = new Property[40];

    // Provide public method
    public Field getBoard(){
        return board[];
    }
}

您应该更加小心地使用变量名。在
main
方法中,有一个名为
board
的变量,但它是先前创建的
board
类的一个实例。它不是数组,因此不能使用
[]
访问其中的元素

我假设您想要访问board类中的
board
数组。但在此之前,您需要改变一些事情:

首先,
board
仅作为局部变量存在于构造函数中,即一旦执行构造函数,数组将超出范围并自动删除。您需要做的是将其设置为Board类中的字段:

public class Board {

    Field board[] = new Property[40];

    public Board() {

        board[0] = new Start();
        board[1] = new Property(1,"Katowice","Mariacka", 600, 100, 175);
        board[2] = new Property(2, "Katowice","Stawowa", 750, 125, 215);
        board[3] = new Property(3, "Katowice","Korfantego", 1000, 150, 250);
    }
}
现在至少可以在构造函数之外使用

然而,这仍然不够,因为默认情况下,
board
将是私有的。因此,您需要一个getter方法:

public Field[] getBoard(){
    return board;
}
然后,您可以执行以下操作:

game.buyProperty(johny, Board.getBoard()[1]);
此外,行中还有一个小的类型错误:

Field board[] = new Property[40];
首先将
board
声明为类型字段的数组,然后将其初始化为类型
属性的数组。这是因为
Property
Field
的子类,但现在只能将
Property
类型的对象放入数组中。假设
Start
不是
Field
的子类,则行
board[0]=newstart()将导致问题。
因此,我建议你简单地写下:

Field board[] = new Field[40];

这样做也更有意义。

这不起作用,因为
board
不是
board
类的公共字段。事实上,它的范围仅限于构造函数,也就是说,一旦构造函数的执行完成,它就会被删除。所以我公开了它。I'v还在main方法中添加了board.board[1]。现在我得到了“错误的第二个参数类型”,它需要属性和找到的字段。。。所以我必须重写整个buyProperty方法?@AGasior好吧,这是可行的,但有点违反了封装原则@阿加西奥·甘博的方法要好得多。是的,我也做到了。我照秋葵的建议做了。不幸的是,我仍然得到了:“错误的第二个参数类型”,找到了…字段,需要…属性。所以我想解决办法是重写我在上面发布的buyProperty方法?我可以简单地将这个方法的第二个参数更改为Field,但是我不能访问我在这个方法中使用的属性类方法。。。属性扩展字段,因此我无法在字段对象上使用属性中的方法。我可以将这些方法移到Field类中,但如果这是正确的,则idk,因为例如getPropertyPrice()只针对Property,而不是所有字段
Field board[] = new Field[40];