正在加载图片...
public class IntegerStack implements Cloneable( private int[ buffer; private int top public IntegerStack(int maxSize b 29 buffer ou bb first 考 top: 1 publ buffer ret second top: 1 publ Ion return super clone; 国■加 IntegerStack first-=new IntegerStack (2); first push (2); first push(9); IntegerStack second=(IntegerStack)first clone(;Shallow Clone • 方法Object.clone()只创建当前对象的影子拷 贝,把这个过程称为影子复制 • 影子复制是在复制过程中只把基本数据类型 的引用类型的值复制到新的对象中 • 源对象与其影子拷贝的基本数据类型的域具 有相同的值,当引用类型的域且共同引用相 同的对象 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() throws CloneNotSupportedException{ return super.clone(); } } IntegerStack first=new IntegerStack(2); first.push(2); first.push(9); IntegerStack second=(IntegerStack)first.clone(); buffer top: 1 buffer top: 1 first second 2 9
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有