【问题标题】:Java - Get/set methods receiving and returning "null"Java - 获取/设置方法接收和返回“null”
【发布时间】:2011-06-04 15:31:37
【问题描述】:

我是 Java 初学者。出于培训目的,我正在尝试为自己构建一个国际象棋游戏应用程序。在我的类 Case 中,这将用于实例化我的所有 64 个案例,我编写了 get/set 方法来查找案例实例中是否有 Piece 占用者。

我读到返回“null”是一种不好的做法,所以我抛出一个异常来表示该案例是免费的。但是,我想知道如何将居住者的指针设置为“null”;当我调用这个方法时,我可以简单地将“null”作为参数吗?

另外,获取/返回“null”是否是一种可接受/良好的做法?

public Piece getOccupant(){
    if (this.occupant == null)
        throw new IllegalArgumentException(this.occupant + " is Empty");
    return this.occupant;
}
public void setOccupant(Piece newOccupant){
    this.occupant = newOccupant;
}

谢谢!

[更新]

感谢大家的 cmets、想法、更正和建议。这是我这部分代码的更新版本,我对它感到满意,因为它达到了它的目的(通过实践增加我的理解)。

/*
 * Modifiers of Occupant
 */
/**
 * Used to find if a Piece is located in this Cell
 * @return a Piece reference to the occupant.  Will send a 
 * null pointer if cell is empty
 */
public Piece getOccupant(){
    return this.occupant;
}
/**
 * Used to set a new occupant in the Cell.
 * @param newOccupant is a reference to a Piece instance, 
 * and should be set to null if the cell is emptied, or using
 * the method clear().
 */
public void setOccupant(Piece newOccupant){
    this.occupant = newOccupant;
}
/**
 * Used to verify if a Cell is empty of any occupant
 * @return true if cell is empty.
 */
public boolean isEmpty(){
    if(this.occupant == null)
        return true;
    return false;
}
/**
 * Free the cell of any occupant, if any were
 */
public void clear(){
    this.occupant = null;
}

【问题讨论】:

  • 也许我错了,但我不明白为什么在这种情况下如果单元格没有居住者返回 null 会很糟糕。 1+ 满足您编写更好代码的愿望!
  • 也许只有我一个人,但是在没有任何参数的方法中抛出 IllegalArgumentException 是不合适的。
  • 请不要有可以返回null的API。这意味着您每次使用时都需要进行检查。

标签: java null get set


【解决方案1】:

板上未被占用的空间也不例外。这是正常的,并且对于大多数董事会成员来说都是如此。你不应该在这里抛出异常;仅应为表示您尝试执行的操作存在重大问题的意外事件引发异常。

您当然可以将 null 传递给 setter(除了基本类型,如 int/long)。

在你的 Space 类中添加一些方便的方法(isEmpty 方法)可能会更好:

public boolean isEmpty(){
   if (this.occupant == null) 
      return true;
   return false;
}

也许还有一个清晰的方法

public void clear() {
    this.occupant = null;
}

这样您就不必测试 getter 结果的无效性,也不必将 null 传递给 set - 这具有易于测试的额外好处,并创建了一个有意义的 API到你的 Space 课程。

【讨论】:

  • 此外,如果您的实现发生变化,您只需在一个位置更改空检查,而不是重构每个调用方法。
  • 可以简化 isEmpty() 函数: public boolean isEmpty() { return this.occupant == null; }
【解决方案2】:

如果你想禁止空值,你应该在setter方法上做:

public void setOccupant(Piece occupant) {
  if (occupant == null) throw new NullPointerException("occupant");
  this.occupant = occupant;
}

请注意,有些人更喜欢抛出 IllegalArgumentException。无论哪种方式,关键是一旦有人设置了禁止值,就“快速失败”。

说了这么多,棋盘当然可以有空位,所以允许 null 似乎更有意义。

我建议您阅读 Josh Bloch 的“Effective Java, 2nd Edition”。

【讨论】:

    【解决方案3】:

    您在哪里阅读了该建议?在我看来,返回null 绝对没有错,只要null 传达了一些有用的信息并且不表示严重的错误情况。在这种情况下,国际象棋单元不包含棋子是完全正常的,我绝对希望getOccupant() 在这种情况下返回 null。

    【讨论】:

      【解决方案4】:

      如果调用者知道 NULL 返回值,那么被调用者返回 NULL 值也不错。

      【讨论】:

        【解决方案5】:

        您应该创建一个类“Empty”、“None”、“Void”,而不是返回 null 或抛出异常,而是将其分配给所有空的 Case。

        【讨论】:

          【解决方案6】:

          小建议 不需要 if 块,你可以通过简单地返回表达式的输出来简化你的代码

          public boolean isEmpty(){
              return this.occupant == null
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-08-27
            • 1970-01-01
            相关资源
            最近更新 更多