c# - What are the practical usage of Cloning -
can give me practical usage clone used, understand how done , types in it, dont see practical usage. read somewhere used save state of object, still think entire state of object important.
if have object y encapsulated (for instance private) within object x, provide accessor method (such gety()) risk returning reference private object , that's not good. don't want client code able reference it. instead might copy or clone object within gety(). mentioned though copying preferred vs. clone -- see effective java item 11.
here example code:
public class x { private date y = new date(); // other code here public date gety() { //this bad: //return y; //this good: return new date(y); }
Comments
Post a Comment