代码之家  ›  专栏  ›  技术社区  ›  Florian

int方法上的表示暴露

  •  0
  • Florian  · 技术社区  · 2 年前

    此函数将暴露worldSize的值,因此它是一种表示暴露形式:

    /**
     * Returns the size of the simulation world.
     * Used for documentation ONLY
     * Should not have representation exposure
     *
     * @return The size of the simulation world.
     */
    public int getWorldSize() 
    {
        int clone = this.worldSize;
        return clone;
    }
    

    我在一个项目中有多个这样的函数,并且只将它们用于文档。我想知道是否有可能在不直接公开整数值的情况下获得相同的结果。

    我过去使用过.clone()或Arrays.copyOf(),但这两种方法都不适用于int类型的函数。

    1 回复  |  直到 2 年前
        1
  •  2
  •   aled    2 年前

    复制基元值

    在这种情况下没有表示形式公开,因为值是基元int类型,而不是对象。该值将自动复制。与对象引用或数组不同,没有可用于修改原始值的共享引用。这只是一个简单的getter函数。您可以将其简化为:

    public int getWorldSize() {
        return this.worldSize;
    }
    
    推荐文章