| 1. | Prudence would dictate that defensive copies are needed when calling them , too 谨慎的做法是,在调用存取器方法时也使用防御性副本。 |
| 2. | You must also be prepared to accept the performance overhead of making defensive copies where necessary 您还要准备着接受在必要时创建防御副本的性能开销。 |
| 3. | And in the absence of clear documentation , you must make defensive copies on both sides of a method call 而缺乏清楚的文档说明时,您必须在方法调用的两边都创建防御副本。 |
| 4. | 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 不可变属性的存取器将总是具有值引用,因而调用的任何一方都不需要防御性复制,这使得它们效率更高。 |
| 5. | Every time an object on the screen moved even the mouse probably seemed daunting at the time , and the overhead of making defensive copies probably seemed out of the question . in hindsight , the decision to make 那时,每当一个对象(甚至是鼠标)在屏幕上移动就创建一个新的point的对象创建开销可能有些让人生畏,而创建防御性副本的开销则不在话下。 |
| 6. | In the absence of clear documentation or a leap of faith , defensive copying is required on both sides of a method call - by the caller , because it doesn t know if the callee will rudely mutate the 由于缺乏清晰的文档说明(或者缺少信任) ,在方法调用的两边都需要创建防御副本调用者需要这样做是因为它不知道被调用者是否会粗暴地改变point ,而被调用者需要这样做是因为它不知道是否保留了对point的引用。 |
| 7. | While the justification for mutability is often based on performance , because there is no need to create a new object every time its state changes , the performance cost of defensive copying can easily outweigh the performance savings from reduced object creation 认为可变性更合理是基于性能方面的考虑,因为不需要在每次状态改变时都创建一个新对象,然而,由防御性复制所招致的性能代价能轻而易举地抵消掉因为减少了对象创建而节省下来的性能。 |
| 8. | Given that so few classes actually include such documentation , the safe strategy when calling a method that doesn t document its call semantics or side - effect behavior is to create a defensive copy before passing it to any such method . ironically , the performance benefit of the decision to make 因为很少有类真正包含这样的文档,所以在调用一个没有用文档说明其调用语义或副作用行为的方法时,安全的策略是在传递它到任何这样的方法之前创建一份防御副本。 |
| 9. | Is called defensive copying , and despite the obvious performance cost , you should get in the habit of using it nearly all the time when returning or storing references to mutable objects or arrays , especially if you are writing a general - purpose facility that may be called by code that you haven t personally written and often even then Getlocation和setlocation的版本3所使用的技术叫做防御性复制( defensive copying ) ,尽管存在着明显的性能上的代价,您也应该养成这样的习惯,即几乎每次返回和存储对可变对象或数组的引用时都使用这一技术,尤其是在您编写一个通用的可能被不是您自己编写的代码调用(事实上这很常见)的工具时更是如此。 |
| 10. | There may be a cost to making defensive copies when passing an array reference to another class , but unless you are sure that the other class either makes its own copy or that it will not hold the reference for longer than the duration of the call , you probably want to make a copy before passing the array 当传递一个数组引用到另一个类时,可能有创建防御副本的代价,除非您能确保其他类要么创建了它自己的副本,要么只在调用期间保存引用,否则您可能需要在传递数组之前创建副本。 |