序列化与对象克隆

package text8;

import java.io.Serializable;//重点
public class Address implements Serializable {
    private static long serialVersionUID=32047567792374973L;
    private String state;
    private String province;
    private String city;

    public void setState(String state) {
        this.state = state;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public Address(String state, String province, String city) {
        this.state = state;
        this.province = province;
        this.city = city;
    }

    // @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("国家 :" + state + ",");
        sb.append("省 " + province + ",");
        sb.append("市 " + city);
        return sb.toString();
    }

    protected Address clone() {
        Address address = null;
        try {
            address = (Address) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return address;
    }
}
View Code
package text8;

import java.io.Serializable;

public class Employee implements Serializable {
    private static long serialVersionUID = 32047567792374983L;
    private String name;
    private int age;
    private Address address;

    public Address getAddress() {
        return address;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public Employee(String name, int age, Address address) {
        this.name = name;
        this.age = age;
        this.address = address;
    }


    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("姓名 " + name + ",");
        sb.append("年龄 " + age + ",");
        sb.append("地址 " + address);
        return sb.toString();
    }
}
View Code
package text8;

import java.io.Serializable;//重点
public class Address implements Serializable {
    private static long serialVersionUID=32047567792374973L;
    private String state;
    private String province;
    private String city;

    public void setState(String state) {
        this.state = state;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public Address(String state, String province, String city) {
        this.state = state;
        this.province = province;
        this.city = city;
    }

    // @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("国家 :" + state + ",");
        sb.append("省 " + province + ",");
        sb.append("市 " + city);
        return sb.toString();
    }

    protected Address clone() {
        Address address = null;
        try {
            address = (Address) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return address;
    }
}
View Code

猜你喜欢

转载自www.cnblogs.com/helloworld2019/p/10604410.html