int 方法的表示暴露

问题描述 投票:0回答:1

这个函数暴露了 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 类型函数。

java oop project representation exposure
1个回答
0
投票

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

public int getWorldSize() 
{
    return this.worldSize;
}
© www.soinside.com 2019 - 2024. All rights reserved.