正在加载图片...
public class IntegerStackimplements Cloneable private private 29 public l buffer buffer op first 29 top: 1 public buffer buffer second public top return public Object cloned t try IntegerStack copy=(IntegerStacksuper clone; copy buffer=(intD)bufferclone; return copy; Icatch(CloneNotSupported Exceptione) throw new InternalErroro; jDeep Clone • 深度复制是指在对象复制过程中除了所有的 基本数据类型的域将被复制,所有的引用类 型的域所引用的对象也将被复制 • 数组对象和类库绝大部分的集合对象都支持 复制机制 • 如果对象包含不可变对象的域,如String类 对象和Wrapper类对象,则无需复制这些域 public class IntegerStack implements Cloneable{ private int[] buffer; private int top; public IntegerStack(int maxSize){ buffer=new int[maxSize]; top=-1; } public void push(int val){ buffer[++top]=val; } public int pop(){ return buffer[top--]; } public Object clone() { try{ IntegerStack copy=(IntegerStack)super.clone(); copy.buffer=(int[])buffer.clone(); return copy; }catch(CloneNotSupportedException e) { throw new InternalError(); } } } buffer top: 1 buffer top: 1 first second 2 9 2 9
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有