一个解决方案是让一个防御性复制你的对象,然后通过你的方法的引用。
One solution is to make a defensive copy of your object, and then pass its reference to your method.
不可变属性的存取器将总是具有值引用,因而调用的任何一方都不需要防御性复制,这使得它们效率更高。
Accessors for immutable properties will always have value semantics and do not need the defensive copying on either side of the call, making them more efficient.
您应该始终使用集合而不是数组来创建不可变列表,尽管防御性的数组复制也是可行的,但这会带来一些不希望见到的副作用。
You should always use collections to create immutable lists rather than arrays. Although it is possible to copy arrays defensively, it leads to some undesirable side-effects.
应用推荐