Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.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_Object_Ownership - Fatal编程技术网

Java 拥有另一个对象的对象

Java 拥有另一个对象的对象,java,object,ownership,Java,Object,Ownership,目前我正在用Java设计一款垄断游戏 游戏中的每个玩家都可以拥有不同的属性。我遇到的问题是如何为每个玩家分配不同的属性对象。我有一个Player类和一个Properties类。写作是最好的方法吗?如果是这样的话,我该怎么做呢?作文很有效。只要玩家有一个properties对象,并且properties对象包含所有必要的数据,您就可以了(假设您实现了必要的getter和setter方法)。该属性可以有一个作为玩家的Owner属性 你也可以在玩家的属性上建立一个列表。用现实世界的术语来思考它 当你在

目前我正在用Java设计一款垄断游戏


游戏中的每个玩家都可以拥有不同的属性。我遇到的问题是如何为每个玩家分配不同的属性对象。我有一个Player类和一个Properties类。写作是最好的方法吗?如果是这样的话,我该怎么做呢?

作文很有效。只要玩家有一个properties对象,并且properties对象包含所有必要的数据,您就可以了(假设您实现了必要的getter和setter方法)。

该属性可以有一个作为玩家的Owner属性


你也可以在玩家的属性上建立一个列表。

用现实世界的术语来思考它

当你在玩“垄断”游戏并购买一处房产时,你会拿着房产卡并将其添加到你面前的房产列表中

因此,在这种情况下,您是一个将属性对象添加到属性列表的玩家对象

public class Player
{
    private List<Property> properties;
}
公共类播放器
{
私有财产清单;
}

您需要组合和多态性

假设一个玩家可以有多个属性,你需要一个属性列表。如果属性具有不同的属性,则可以应用多态性和继承。您可能只会看到下面的继承,但是当您获取不同的属性并对它们进行操作时,您将需要多态性

大体上:

public static void main(String args[]){
  Player player1 = new Player();

  BlackProperty blackProperty = new BlackProperty();
  BlueProperty blueProperty = new BlueProperty();

  player1.addProperty(blackProperty);
  player1.addProperty(blueProperty);
}
您的所有域类:

public class Player{
  private List<Properties> propertyList;

  // getters and setters

  public void addProperty(Properties properties){
    if(this.propertyList == null){
      this.propertyList = new ArrayList<Properties>();
    }

    this.propertyList.add(properties);
  }
}

public class Properties{
  private int noOfFloor;
  // getters and setters
}

public class BlackProperty extend Properties{
  private String noOfGate;
  // getters and setters
}

public class BlueProperty extend Properties{
  private String noOfLawn;
  // getters and setters
}
公共类播放器{
私有列表propertyList;
//接球手和接球手
公共void addProperty(属性){
if(this.propertyList==null){
this.propertyList=新的ArrayList();
}
this.propertyList.add(属性);
}
}
公共类属性{
内特诺夫勒私人酒店;
//接球手和接球手
}
公共类BlackProperty扩展属性{
私有字符串noOfGate;
//接球手和接球手
}
公共类BlueProperty扩展属性{
私有字符串Noon;
//接球手和接球手
}

我想添加一个新的类PropertyManager

这使得您可以在单个位置轻松地提供业务规则(良好的关注点分离),而不必在选择组合的情况下浏览一堆播放器或属性对象。这将防止玩家和/或财产类别在未来因买卖业务规则而变得沉重

public final class PropertyManager {

  /**
   * The PropertyManager instance, example usage:
   *   PropertyManager.INSTANCE.buyProperty(property, buyer);
   * NB: Good candidate for dependency injection instead if you are doing this.
   */
  public static final PropertyManager INSTANCE = new PropertyManager();

  private static final Map<Property, Player> propertyListing = new HashMap<Property, Player>();

  /**
   * Buy a property from the banker, banker or property manager could maintain
   * Collection of available properties
   */
  public void buyProperty(Player buyer, Property property) {
    if (propertyListing.containsKey(property)) {
      throw new IllegalStateException("Unable to buy unless owner sells it");
    }
    propertyListing.put(property, buyer);
  }

  /**
   * Broker a transaction between two players for the sale of a property
   */
  public void sellProperty(Player seller, Player buyer, Property property) {
    if (!propertyListing.containsKey(property)) {
      throw new IllegalStateException("Unable to sell Property which is not owned");
    }
    Player owner = propertyListing.get(property);
    if (!owner.equals(seller)) {
      throw new IllegalStateException("Unable to sell property seller doesn't own");
    }
    // TODO : Deduct/transfer monies (ensure sufficient value in buyer account etc)
    propertyListing.put(property, buyer); 
  }

  /**
   * Retrieve a List of all of the Player's properties
   */
  public List<Property> getProperties(Player owner) {
    // TODO Either iterate through the propertyListing or use a second Map for player to List<Property>, NB: they should be guarded with a shared lock if you do this (threading).
  }

  /**
   * Retrieve the owner of a Property or null if it is unowned
   */
  @Nullable // javax annotation indiciates can be null
  public Player getOwner(Property property) {
    return propertyListing.get(property);
  }

  /**
   * Hide the constructor as the only property manager should be INSTANCE
   */
  private PropertyManager() {
    // Prevent further instantiation
  }
}
公共最终类PropertyManager{
/**
*PropertyManager实例,示例用法:
*PropertyManager.INSTANCE.buyProperty(property,buyer);
*注意:如果您正在这样做的话,那么就应该选择依赖项注入。
*/
公共静态最终PropertyManager实例=新PropertyManager();
私有静态最终映射PropertyList=new HashMap();
/**
*从银行家、银行家或物业经理处购买物业
*可用属性的集合
*/
公共无效购买财产(玩家买家、财产){
if(propertyListing.containsKey(property)){
抛出新的非法状态例外(“除非所有者出售,否则无法购买”);
}
房地产上市。卖出(房地产、买方);
}
/**
*经纪人两个参与者之间为出售房产而进行的交易
*/
公共无效出售财产(玩家卖家、玩家买家、财产){
如果(!PropertyList.containsKey(属性)){
抛出新的非法状态例外(“无法出售未拥有的财产”);
}
玩家所有者=propertyListing.get(属性);
如果(!所有者等于(卖方)){
抛出新的非法状态例外(“无法出售卖方不拥有的财产”);
}
//TODO:扣除/转账款项(确保买方账户等有足够的价值)
房地产上市。卖出(房地产、买方);
}
/**
*检索玩家所有属性的列表
*/
公共列表getProperties(玩家所有者){
//TODO可以遍历PropertyList,也可以使用第二个映射供玩家列出,注意:如果您这样做(线程化),则应该使用共享锁来保护它们。
}
/**
*检索属性的所有者,如果没有所有者,则为null
*/
@可空//javax注释指示可以为空
公共播放器getOwner(属性){
返回PropertyList.get(属性);
}
/**
*隐藏构造函数,因为唯一的属性管理器应该是实例
*/
私有属性管理器(){
//防止进一步实例化
}
}

写作是实现这一目标的唯一途径。收藏会很好。你尝试了什么?你被困在哪里了?我还没有真正尝试过任何东西,但我已经在脑海里玩过了。最初我计划在每个属性中都有一个int类型的所有者变量。然后,所有者将由玩家的索引值确定(因为他们已被放入列表中)。我觉得这种方法不是很有效,也不是很有限。这是一个很好的观察结果,但在现实世界中,存在着软件中不存在的物理约束(比如只有一张物理卡)。如果你在玩家或财产中进行合成,那么你可能会在管理财产交易等方面给这些POJO引入复杂性。我喜欢你的答案。我一直在寻找更好的方法来设计代码,您的示例已经清楚地说明了这一点。:)谢谢你,伙计,很高兴能帮上忙!我认为这是一个很好的观点,应该可以简化我将遇到的管理属性的不同复杂性。