本帖最后由 上篮高手 于 2013-10-18 19:54 编辑
假克隆:
对于基本类型可以使用 = 来进行克隆,此时两个变量除了相等没有任何关系,
但对于引用类型却不能简单的使用 = 进行克隆,这主要是于 java的内存空间使用有关。
通常情况很少使用 = 来操作对象,也很少使用 == 来比较两个对象。
定义一个Employee类: 如下- package com.mingrisoft.oop;
- public class Employee {
- private String name;
- private int age;
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public int getAge() {
- return age;
- }
-
- public void setAge(int age) {
- this.age = age;
- }
-
- @Override
- public String toString() {
- return "姓名:" + name + ", 年龄:" + age;
- }
- }
复制代码 以下是main函数:- package com.mingrisoft.oop;
- public class Test {
- public static void main(String[] args) {
- System.out.println("克隆之前:");
- Employee employee1 = new Employee();
- employee1.setName("java1");
- employee1.setAge(12);
- System.out.println("员工1的信息:");
- System.out.println(employee1);
-
-
-
- System.out.println("克隆之后:");
- Employee employee2 = employee1;
- employee2.setName("web1");
- employee2.setAge(34);
- System.out.println("员工2的信息:");
- System.out.println(employee2);
-
- System.out.println("员工1的信息:");
- System.out.println(employee1);
- }
- }
复制代码 打印信息如下:- 克隆之前:
- 员工1的信息:
- 姓名:java1, 年龄:12
- 克隆之后:
- 员工2的信息:
- 姓名:web1, 年龄:34
- 员工1的信息:
- 姓名:web1, 年龄:34
复制代码 当修改Employee2 的时候 employmee1 的内容也被修改了。并没有达到克隆目的。
浅克隆如下:- package com.mingrisoft.oop;
- <font color="#ff0000">public class Employee implements Cloneable {</font>
- private String name;
- private int age;
- private Address address; <font color="#ff0000"> //此变量是一个自定义里变量。</font>
-
- public Employee(String name, int age, Address address) {
- this.name = name;
- this.age = age;
- this.address = address;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public int getAge() {
- return age;
- }
-
- public void setAge(int age) {
- this.age = age;
- }
-
- public Address getAddress() {
- return address;
- }
-
- public void setAddress(Address address) {
- this.address = address;
- }
-
- @Override
- public String toString() {
- StringBuilder sb = new StringBuilder();
- sb.append("姓名:" + name + ", ");
- sb.append("年龄:" + age + "\n");
- sb.append("地址:" + address);
- return sb.toString();
- }
-
- <font color="#ff0000"> @Override
- public Employee clone() {
- Employee employee = null;
- try {
- employee = (Employee) super.clone();
- } catch (CloneNotSupportedException e) {
- e.printStackTrace();
- }
- return employee;
- }</font>
- }
复制代码 main函数如下:- package com.mingrisoft.oop;
- public class Test {
- public static void main(String[] args) {
- System.out.println("克隆之前:");
- Address address = new Address("河北", "邯郸", "丛台");
- Employee employee1 = new Employee("占二", 12, address);
- System.out.println("员工1的信息:");
- System.out.println(employee1);
-
-
- System.out.println("克隆之后:");
- Employee employee2 = employee1.clone();
- employee2.getAddress().setState("中国");
- employee2.getAddress().setProvince("云南");
- employee2.getAddress().setCity("昆明");
- employee2.setName("国美");
- employee2.setAge(114);
-
- System.out.println("员工2的信息:");
- System.out.println(employee2);
- System.out.println("员工1的信息:");
- System.out.println(employee1);
- }
- }
复制代码 打印信息如下:- 克隆之前:
- 员工1的信息:
- 姓名:占二, 年龄:12
- 地址:国家:河北, 省:邯郸, 市:丛台
- 克隆之后:
- 员工2的信息:
- 姓名:国美, 年龄:114
- 地址:国家:中国, 省:云南, 市:昆明
- 员工1的信息:
- 姓名:占二, 年龄:12
- 地址:国家:中国, 省:云南, 市:昆明
复制代码 分析可见 : 占二的姓名和年龄不没有被修改,但是地址信息被更改了。 因为 String int 都可以实现值拷贝
有于自定义Address 类没有实现 Cloneable 接口所以,不能实现克隆,而是指向了同一个对象。
深克隆如下:- package com.mingrisoft.oop;
- <font color="#ff0000">public class Address implements Cloneable {</font>
- private String state;
- private String province;
- private String city;
-
- public Address(String state, String province, String city) {
- this.state = state;
- this.province = province;
- this.city = city;
- }
-
- public String getState() {
- return state;
- }
-
- public void setState(String state) {
- this.state = state;
- }
-
- public String getProvince() {
- return province;
- }
-
- public void setProvince(String province) {
- this.province = province;
- }
-
- public String getCity() {
- return city;
- }
-
- public void setCity(String city) {
- this.city = city;
- }
-
- @Override
- public String toString() {
- StringBuilder sb = new StringBuilder();
- sb.append("国家:" + state + ", ");
- sb.append("省:" + province + ", ");
- sb.append("市:" + city);
- return sb.toString();
- }
-
- <font color="#ff0000"> @Override
- protected Address clone() {
- Address address = null;
- try {
- address = (Address) super.clone();
- } catch (CloneNotSupportedException e) {
- e.printStackTrace();
- }
- return address;
- }</font>
- }
复制代码- package com.mingrisoft.oop;
- <font color="#ff0000">public class Employee implements Cloneable {</font>
- private String name;
- private int age;
- private Address address;
-
- public Employee(String name, int age, Address address) {
- this.name = name;
- this.age = age;
- this.address = address;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public int getAge() {
- return age;
- }
-
- public void setAge(int age) {
- this.age = age;
- }
-
- public Address getAddress() {
- return address;
- }
-
- public void setAddress(Address address) {
- this.address = address;
- }
-
- @Override
- public String toString() {
- StringBuilder sb = new StringBuilder();
- sb.append("姓名:" + name + ", ");
- sb.append("年龄:" + age + "\n");
- sb.append("地址:" + address);
- return sb.toString();
- }
-
- <font color="#ff0000"> @Override
- public Employee clone() {
- Employee employee = null;
- try {
- employee = (Employee) super.clone();</font>
- <font color="#9acd32"> employee.address = address.clone();</font>
- <font color="#ff0000"> } catch (CloneNotSupportedException e) {
- e.printStackTrace();
- }
- return employee;
- }</font>
- }
复制代码 打印信息如下:
克隆之前:
员工1的信息:
姓名:占二, 年龄:12
地址:国家:河北, 省:邯郸, 市:丛台
克隆之后:
员工2的信息:
姓名:国美, 年龄:114
地址:国家:中国, 省:云南, 市:昆明
员工1的信息:
姓名:占二, 年龄:12
地址:国家:河北, 省:邯郸, 市:丛台
占二的信息并没有被更改,从而实现了深克隆。
|