首页 > 代码库 > Clone

Clone

浅克隆

package fun;
public class b extends a implements Cloneable {
public static void main(String[] args) throws CloneNotSupportedException {
b b=new b();
b b1=(fun.b) b.clone();//调用clone的方法,返回Object类,需要强转
b1.fun();
b.fun();
}

private void fun() {
System.out.println("123");
}
@Override//复写clone的方法
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}

Clone