简体   繁体   English

Java通过数组访问对象

[英]Java accessing object throught array

I'm trying to make Monopoly/Eurbuisness game in Java, but I got stuck with kinda simple problem... So, I have basic Field class 我试图用Java制作Monopoly / Eurbuisness游戏,但遇到了一个简单的问题……所以,我有基本的Field类

public class Field {
    protected int fieldPosition;

    Field(){    }
}

then I extend this class to subclasses like Start, Property, Jail, Chance, etc. 然后将此类扩展到诸如Start,Property,Jail,Chance等子类。

Currently I'm working on Property class 目前我正在研究房地产课

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

Next class is Board, it should create basic table of all fields (using polymorphysm, some of them will be Properties, some will be something else) 下一个类是Board,它应该创建所有字段的基本表(使用polymorphysm,其中一些将是Properties,一些将是其他东西)

    public class Board {
    public Board() {
        Field board[] = new Property[40];

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

Then in Game class I got method buyProperty 然后在游戏类中,我有方法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());
    }
}

Finnaly, Im trying to test it but unfortunatelly, it doesnt work 好的,我正在尝试对其进行测试,但不幸的是,它不起作用

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: array required, but game.components.Board found java:需要数组,但找到game.components.Board

and it points into last line I posted 它指向我发布的最后一行

any ideas, suggestions? 有什么想法,建议吗?

You should be more carefull with your variable names. 您应该更加谨慎地使用变量名。 In your main method, you have a variable named board - but it's an instance of the Board class you created earlier. 在您的main方法中,您有一个名为board的变量-但这是您之前创建的Board类的实例。 It is not an array, therefore you cannot use the [ ] to access elements in it. 它不是数组,因此不能使用[ ]访问其中的元素。

I assume you want to access the board array inside the board class. 我假设您要访问board类中的board数组。 But before you can do that, you need to change a few things: 但是在执行此操作之前,您需要更改以下几项:

First of all, board only exists as a local variable in the constructor, ie once your constructor is executed the array is out of scope and will be deleted automatically. 首先, board仅在构造函数中作为局部变量存在,即,一旦执行构造函数,数组将超出范围,将被自动删除。 What you need to do instead is make it a field in the Board class: 您需要做的是使其成为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);
    }
}

Now at least you can use board outside of the constructor. 现在至少您可以在构造函数之外使用board

That, however is still not enough since board is going to be private by default. 但是,这还不够,因为默认情况下board将是私有的。 So you need a getter method for it: 因此,您需要一种getter方法:

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

Then you can do something like: 然后,您可以执行以下操作:

game.buyProperty(johny, Board.getBoard()[1]);

Also, you have a little type error in the line: 此外,您在该行中还存在一些类型错误:

Field board[] = new Property[40];

You first declare board as an array of the type Field, then initialize it as an array of the type Property . 首先,将board声明为Field类型的数组,然后将其初始化为Property类型的数组。 That works because Property is a subclass of Field but now you can only put objects of the type Property in the array. 之所以Property是因为PropertyField的子类,但是现在您只能将Property类型的对象放入数组中。 Assuming Start is not a subclass of Field , the line board[0] = new Start(); 假设Start不是Field的子类,则线路board[0] = new Start(); will cause problems. 会造成问题。 So I would suggest you simply write: 所以我建议您简单地写:

Field board[] = new Field[40];

It also makes more sense that way. 这样也更有意义。

This is a Board class containing Field board[] 这是一个包含Field board[]Board类。

Board board = new Board();

So yes, the following line won't work because board is a Board class, not the Property[] Array inside the Board class. 所以,是的,下面一行是行不通的,因为boardBoard类,而不是Property[] Array里面Board级。

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

I suppose what you want to achieve is 我想你想要实现的是

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

Edit: thanks for the correction from Gumbo. 编辑:感谢来自Gumbo的更正。

You should consider move Field board[] out of constructor to Board class, and then provide a public method to access Field board[] : 您应该考虑将Field board[]从构造函数中移到Board类,然后提供一个公共方法来访问Field board[]

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM