zxy0307's picture
Upload 2300 files
0284885 verified
Raw History Blame Contribute Delete
932 Bytes
package Object_package;
public class Object_demo3 {
public static void main(String[] args) throws CloneNotSupportedException {
//protected Object clone(int a) 对象克隆
//1、先创建一个对象
int[] data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0};
User u1 = new User(1, "张三", "1234qwer", "girl111", data);
//2、克隆对象
//细节:
//方法在底层会帮我们创建一个对象,并把原对象中的数据拷贝过去
//书写细节:
//1、重写Object中的clone方法
//2、让javabean类实现Cloneable接口
//3、创建原对象并调用clone方法就可以了
User u2 = (User) u1.clone();
//验证一件事情:Object中的克隆是浅克隆
int[] arr = u1.getData();
arr[0] = 100;
System.out.println(u1);
System.out.println(u2);
}
}