首页 > 编程语言 > 详细

java学习---对象的克隆和序列化

时间:2014-06-15 16:32:27      阅读:381      评论:0      收藏:0      [点我收藏+]

1、假clone,虚拟机只是将对象的内存地址clone了一份,修改克隆后的对象内容,将影响原对象

public class Employee{
    public Employee(){
 
    }
 
    public Employee(String name, int age){
        this.age = age;
        this.name = name;
    }
 
    @Override
    public String toString(){
        return "姓名: " + name + "年龄: " + 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;
    }
 
    public static void main(String[] args){
        Employee demo1 = new Employee("rollen", 20);
        System.out.println(demo1);
        Employee demo2 = demo1;
        demo2.setAge(100);
        demo2.setName("hello world");
        System.out.println(demo1);
        System.out.println(demo2);
    }
 
    private String name;
    private int age;
}
运行结果:

【运行结果】

姓名: rollen年龄: 20

姓名: hello world年龄: 100

姓名: hello world年龄: 100

============================================================================================

2、含有基本数据类型的对象的克隆,必须实现clone方法。克隆后对象属性的修改将不影响原对象的属性

class Address{
    public Address(){
 
    }
 
    public Address(String state, int number){
        this.number = number;
        this.state = state;
    }
 
    @Override
    public String toString(){
        return "state: " + state + " munber: " + number;
    }
 
    public String getState(){
        return state;
    }
 
    public void setState(String state){
        this.state = state;
    }
 
    public int getNumber(){
        return number;
    }
 
    public void setNumber(int number){
        this.number = number;
    }
 
    private String state;
    private int number;
}
 
public class Employee implements Cloneable{
    public Employee(){
 
    }
 
    public Employee(String name, int age, Address address){
        this.address = address;
        this.age = age;
        this.name = name;
    }
 
    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:" + name + ", ");
        sb.append("age:" + age + " \n");
        sb.append("Address: " + address);
        return sb.toString();
    }
 
    @Override
    protected Employee clone(){
        Employee employee = null;
        try{
            employee = (Employee) super.clone();
        }catch(CloneNotSupportedException e){
            e.printStackTrace();
        }
        return employee;
    }
 
    public static void main(String[] args){
        System.out.println("克隆之前:");
        Address add1 = new Address("中国", 1);
        Employee emp1 = new Employee("rollen", 20, add1);
        System.out.println(emp1);
        System.out.println("克隆之后");
        Employee emp2 = emp1.clone();
        emp2.setName("hello world");
        emp2.setAge(100);
        emp2.address.setNumber(2);
        emp2.address.setState("美国");
        System.out.println(emp1);
        System.out.println("-----");
        System.out.println(emp2);
    }
 
    private String name;
    private int age;
    private Address address;
}

【运行结果】:

克隆之前:

name:rollen, age:20

Address: state: 中国 munber: 1

克隆之后

name:rollen, age:20

Address: state: 美国 munber: 2

-----

name:hello world, age:100

Address: state: 美国 munber: 2

==============================================================================================

3、含有复制数据结构类的类的克隆,对类中复杂的数据类型也必须实现clone方法。

class Address implements Cloneable{
    public Address(){
 
    }
 
    public Address(String state, int number){
        this.number = number;
        this.state = state;
    }
 
    @Override
    public String toString(){
        return "state: " + state + " munber: " + number;
    }
 
    @Override
    protected Address clone() throws CloneNotSupportedException{
        Address address = null;
        address = (Address) super.clone();
        return address;
    }
 
    public String getState(){
        return state;
    }
 
    public void setState(String state){
        this.state = state;
    }
 
    public int getNumber(){
        return number;
    }
 
    public void setNumber(int number){
        this.number = number;
    }
 
    private String state;
    private int number;
}
 
public class Employee implements Cloneable{
    public Employee(){
 
    }
 
    public Employee(String name, int age, Address address){
        this.address = address;
        this.age = age;
        this.name = name;
    }
 
    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:" + name + ", ");
        sb.append("age:" + age + " \n");
        sb.append("Address: " + address);
        return sb.toString();
    }
 
    @Override
    protected Employee clone(){
        Employee employee = null;
        try{
            employee = (Employee) super.clone();
            employee.address = address.clone();     }catch(CloneNotSupportedException e){
            e.printStackTrace();
        }
        return employee;
    }
 
    public static void main(String[] args){
        System.out.println("克隆之前:");
        Address add1 = new Address("中国", 1);
        Employee emp1 = new Employee("rollen", 20, add1);
        System.out.println(emp1);
        System.out.println("克隆之后");
        Employee emp2 = emp1.clone();
        emp2.setName("hello world");
        emp2.setAge(100);
        emp2.setAddress(new Address("美国", 2));
        System.out.println(emp1);
        System.out.println("-----");
        System.out.println(emp2);
    }
 
    private String name;
    private int age;
    private Address address;
}

【运行结果】:

克隆之前:

name:rollen, age:20

Address: state: 中国 munber: 1

克隆之后

name:rollen, age:20

Address: state: 中国 munber: 1

-----

name:hello world, age:100

Address: state: 美国 munber: 2

=========================================================================================

4、序列化接口和对象克隆

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
 
class Address implements Serializable{
    public Address(){
 
    }
 
    public Address(String state, int number){
        this.number = number;
        this.state = state;
    }
 
    @Override
    public String toString(){
        StringBuilder sb = new StringBuilder();
        sb.append("number: " + number);
        sb.append("state" + state + "\n");
        return sb.toString();
    }
 
    public String getState(){
        return state;
    }
 
    public void setState(String state){
        this.state = state;
    }
 
    public int getNumber(){
        return number;
    }
 
    public void setNumber(int number){
        this.number = number;
    }
 
    private String state;
    private int number;
}
 
public class Employee implements Cloneable, Serializable{
    public Employee(){
 
    }
 
    public Employee(String name, int age, Address address){
        this.address = address;
        this.age = age;
        this.name = name;
    }
 
    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:" + name + ", ");
        sb.append("age:" + age + " \n");
        sb.append("Address: " + address);
        return sb.toString();
    }
 
    @Override
    protected Employee clone(){
        Employee employee = null;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try{
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(this);
            oos.close();
 
            ByteArrayInputStream bais = new ByteArrayInputStream(
                    baos.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(bais);
            employee = (Employee) ois.readObject();
            ois.close();
        }catch(Exception e){
            e.printStackTrace();
        }
        return employee;
    }
 
    public static void main(String[] args){
        System.out.println("克隆之前:");
        Address add1 = new Address("中国", 1);
        Employee emp1 = new Employee("rollen", 20, add1);
        System.out.println(emp1);
        System.out.println("克隆之后");
        Employee emp2 = emp1.clone();
 
        emp2.setName("hello world");
        emp2.setAge(100);
        emp2.address.setNumber(2);
        emp2.address.setState("美国");
        System.out.println(emp1);
        System.out.println("-----");
        System.out.println(emp2);
    }
 
    private String name;
    private int age;
    private Address address;
}

克隆之前:

name:rollen, age:20

Address: number: 1state中国

 

克隆之后

name:rollen, age:20

Address: number: 1state中国

 

-----

name:hello world, age:100

Address: number: 2state美国




java学习---对象的克隆和序列化,布布扣,bubuko.com

java学习---对象的克隆和序列化

原文:http://blog.csdn.net/yyxhhx/article/details/30320253

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!