我试图用Java制作Monopoly / Eurbuisness游戏,但我遇到了一个简单的问题...... 所以,我有基本的Field类
public class Field {
protected int fieldPosition;
Field(){ }
}
然后我将这个类扩展到子类,如Start,Property,Jail,Chance等。
目前我正在处理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();
}
下一课是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);
}
}
然后在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());
}
}
Finnaly,我试图测试它,但不幸的是,它不起作用
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,但找到了game.components.Board
它指向我发布的最后一行
任何想法,建议?
答案 0 :(得分:1)
您应该更加小心您的变量名称。在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
默认为私有,因此仍然不够。所以你需要一个getter方法:
public Field[] getBoard(){
return board;
}
然后你可以做类似的事情:
game.buyProperty(johny, Board.getBoard()[1]);
此外,您在行中有一点类型错误:
Field board[] = new Property[40];
首先将board
声明为Field类型的数组,然后将其初始化为类型Property
的数组。这是有效的,因为Property
是Field
的子类,但现在您只能将类型为Property
的对象放入数组中。假设Start
不是Field
的子类,则行board[0] = new Start();
将导致问题。
所以我建议你简单写一下:
Field board[] = new Field[40];
这样也更有意义。
答案 1 :(得分:0)
这是一个包含Board
Field board[]
类
Board board = new Board();
所以是的,以下行不会起作用,因为board
是Board
类,而不是Property[] Array
类中的Board
。
game.buyProperty(johny, board[1]);
我想你想要实现的是
game.buyProperty(johny, board.getBoard()[1]);
编辑:感谢Gumbo的修正。
您应该考虑将Field board[]
从构造函数移到Board类,然后提供一个公共方法来访问Field board[]
:
public class Board {
// Move board
Field board[] = new Property[40];
// Provide public method
public Field getBoard(){
return board[];
}
}