gpt4 book ai didi

Java通过数组访问对象

转载 作者:行者123 更新时间:2023-12-01 09:02:54 25 4
gpt4 key购买 nike

我正在尝试用 Java 制作 Monopoly/Eurbuisness 游戏,但我遇到了一个简单的问题......所以,我有基本的 Field 类

public class Field {
protected int fieldPosition;

Field(){ }
}

然后我将此类扩展为子类,例如 Start、Property、Jail、Chance 等。

目前我正在研究 Property 类

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,它应该创建所有字段的基本表(使用多态性,其中一些将是属性,一些将是其他内容)

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

然后在 Game 类中我得到了方法 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: array required, but game.components.Board found

它指向我发布的最后一行

有什么想法、建议吗?

最佳答案

您应该更加小心变量名称。在您的 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

但这仍然不够,因为 board 默认情况下将是私有(private)的。所以你需要一个 getter 方法:

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

然后你可以这样做:

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

此外,该行中存在一些类型错误:

Field board[] = new Property[40];

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

Field board[] = new Field[40];

这样也更有意义。

关于Java通过数组访问对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41534433/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com