我在基类Emp中,是这样写的:
public Object Clone()
  throws CloneNotSupportedException{
Emp e = new Emp();
e.setName(new String(this.getName()));
return e;
}
现Emp有一子类,比如说是sells,有一个属性String tel,我也想实现tel的Clone,请问该怎么写,最好是能不要全部重写clone。或者说要怎么写Emp的clone,还可以让sells的clone不要全部重写就可以实现clone。

解决方案 »

  1.   

    Clone暂时我给你提供有三种方式:1.首先是通过Clone方法来实现,但是要实现标记型接口Cloneable
    package chuangsi_0412.all.test;/**
     * clone
     * 
     * @author Administrator
     * 
     */
    public class TestClone {
    public static void main(String[] args) throws CloneNotSupportedException {
    Contact con = new Contact("13548696678", "[email protected]");
    Person p1 = new Person(1, con); Person p3 = (Person) p1.clone();
    System.out.println(p3.id + " " + p3.con.email);
    Contact con2=(Contact) con.clone();
    System.out.println(con2.email+" "+con2.tel );

    }
    }class Person implements Cloneable {
    int id; Contact con; public Person(int id, Contact con) {
    this.id = id;
    this.con = con;
    } public Object clone() throws CloneNotSupportedException {
    Person p = (Person) super.clone();
    p.con = (Contact) this.con.clone();
    return p;
    }}class Contact implements Cloneable{
    String tel; String email; public Contact(String tel, String email) {
    this.tel = tel;
    this.email = email;
    } public Object clone() throws CloneNotSupportedException {
    Contact c = (Contact) super.clone();
    return c;
    }}
      

  2.   

    2.第二种是自己定义一个克隆方法
    public class User {
    /* 顶级对象 */
    public int id;
    public String name;

    /* 非顶级对象 */
    Location loc;

    public User(int id, String name,Location loc) {
    this.id = id;
    this.name = name;
    this.loc=loc;
    } public User getCopy(final User olduser) {
    Location newLoc=new Location(olduser.loc.address);
    User x = new User(olduser.id, olduser.name,newLoc);
    return x;
    } public boolean equals(Object obj) {
    User x = (User) obj;
    return id == x.id ? true : false;
    }}
    public class Location {
    public String address;

    public Location(String address){
    this.address=address;
    }

    public Location getCopy(final Location loc){
    Location e=new Location(loc.address);
    return e;
    }
    }
    public class TestCopy {
    public static void main(String args[]){
    Location l1=new Location("address!!");
    User x1=new User(0,"kk1",l1);
    User x2=x1.getCopy(x1);

    // x1.id=123;
    // x1.loc.address="diyi dizhi";
    // x2.id=234;
    // x2.loc.address="dier dizhi";
    //
    // System.out.println("-------------");
    // System.out.println(x1==x2);
    // System.out.println(x1.id);
    // System.out.println(x1.loc.address);
    // System.out.println(x2.id);
    // System.out.println(x2.loc.address);

    System.out.println("-------------");
    User x3=x1;
    System.out.println(x3==x1);
    System.out.println(x1);
    System.out.println(x1.hashCode());
    System.out.println(x3);
    System.out.println(x3.hashCode());


    }
    }
      

  3.   

    3:第三种是通过Serlizable标记型接口来实现,这个也最简单,逻辑比较清晰
    就是把当前对象写入到一块内存,然后再拿出来重新组装,就实现了对象的Clone
    public class Persion {
    private int id;
    private String name;
    private int age; public int getAge() {
    return age;
    } public void setAge(int age) {
    this.age = age;
    } public int getId() {
    return id;
    } public void setId(int id) {
    this.id = id;
    } public String getName() {
    return name;
    } public void setName(String name) {
    this.name = name;
    } // Object To Byte[]
    public byte[] object2byte() {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos); try {
    dos.writeInt(this.id);
    dos.writeUTF(this.name);
    dos.writeInt(this.age);
    return baos.toByteArray(); } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } finally {
    try {
    if (dos != null) {
    dos.close();
    }
    if (baos != null) {
    baos.close();
    }
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    return null;
    } // byte[] to object no cache
    public static Persion byte2object(byte[] buf) {
    return byte2obejct(buf, 0, buf.length);
    } // byte[] to object yes cache
    public static Persion byte2obejct(byte[] buf, int offset, int length) {
    ByteArrayInputStream bais = new ByteArrayInputStream(buf, offset,
    length);
    DataInputStream dis = new DataInputStream(bais); Persion p;
    try { p = new Persion();
    p.setId(dis.readInt());
    p.setName(dis.readUTF());
    p.setAge(dis.readInt());
    return p;
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } finally {
    try {
    if (dis != null) {
    dis.close();
    }
    if (bais != null) {
    bais.close();
    }
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    return null; }}
    package chuangsi_0424.io.bytearray;import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;public class TestByteArray { /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    // Persion p=new Persion();
    // p.setId(100);
    // p.setName("我是天才");
    // p.setAge(20);
    //
    // byte[] buf=p.object2byte();
    //
    // try {
    // FileOutputStream fos=new FileOutputStream("D:/persion.dat");
    // fos.write(buf);
    //
    // fos.close();
    // } catch (FileNotFoundException e) {
    // // TODO Auto-generated catch block
    // e.printStackTrace();
    // } catch (IOException e) {
    // // TODO Auto-generated catch block
    // e.printStackTrace();
    // }


    FileInputStream fis = null;
    try {
    fis = new FileInputStream("D:/persion.dat");
    byte[] buf = new byte[1024]; int length = fis.read(buf); Persion p = Persion.byte2obejct(buf, 0, length);
    System.out.println(p.getAge());
    System.out.println(p.getId());
    System.out.println(p.getName()); } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } finally {
    try {
    fis.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    } }}
      

  4.   


    public class Emp {
    private String name; public String getName() {
    return name;
    } public void setName(String name) {
    this.name = name;
    } public Object Clone() throws CloneNotSupportedException {
    Object o = null;
    try {
    o = this.getClass().newInstance();  //不同子类调用是生成相应对象
    ((Emp) o).setName(new String(this.getName()));
    } catch (Exception e) {
    }
    return o;
    }
    }
    public class Sells extends Emp{
    private String tel; public String getTel() {
    return tel;
    } public void setTel(String tel) {
    this.tel = tel;
    }
    public Object Clone() throws CloneNotSupportedException {
    Object o = super.Clone();  //调用父类方法
    ((Sells)o).setTel(this.tel);//子类自己要实现的功能
    return o;
    }
    public static void main(String[] args) throws CloneNotSupportedException{
    Sells s = new Sells();
    s.setName("abc");
    s.setTel("123");
    Sells s1 = (Sells)s.Clone();
    System.out.println(s1.getName());
    System.out.println(s1.getTel());
    }
    }
      

  5.   

    4:第四种是对第三种的补充通过serlizable来实现
    package chuangsi_0424.io.serlizable;import java.io.Serializable;public class Address implements Serializable {
    private static final long serialVersionUID = 1L;
    private String city;
    private String street; public Address() {
    super();
    } public Address(String city, String street) {
    super();
    this.city = city;
    this.street = street;
    } public String getCity() {
    return city;
    } public void setCity(String city) {
    this.city = city;
    } public String getStreet() {
    return street;
    } public void setStreet(String street) {
    this.street = street;
    }}package chuangsi_0424.io.serlizable;import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.Serializable;public class Persion implements Serializable {
    private static final long serialVersionUID = 1L;
    private int id;
    private String name;
    private int age;
    private Address address;

    public Address getAddress() {
    return address;
    }
    public void setAddress(Address address) {
    this.address = address;
    }
    public int getAge() {
    return age;
    }
    public void setAge(int age) {
    this.age = age;
    }
    public int getId() {
    return id;
    }
    public void setId(int id) {
    this.id = id;
    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public Persion(int id, String name, int age, Address address) {
    super();
    this.id = id;
    this.name = name;
    this.age = age;
    this.address = address;
    }

    public Persion() {
    super();
    }

    public Object getcopy() {
    // 得到全部的byte流,然后进行byte流的拷贝,最后读入到Object 流进行拷贝 Object temp = null;
    // 1。先将本个对象存入到Object流
    ByteArrayOutputStream baos = null;
    ByteArrayInputStream bais = null;
    ObjectOutputStream oos = null;
    ObjectInputStream ois = null;
    try {
    baos = new ByteArrayOutputStream();
    oos = new ObjectOutputStream(baos); oos.writeObject(this);
    byte[] copy = baos.toByteArray(); bais = new ByteArrayInputStream(copy, 0, copy.length);
    ois = new ObjectInputStream(bais); temp = ois.readObject();
    return temp;
    } catch (IOException e) {
    e.printStackTrace();
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } finally {
    try {
    if (baos != null) {
    baos.close();
    }
    if (bais != null) {
    bais.close();
    }
    if (oos != null) {
    oos.close();
    }
    if (ois != null) {
    ois.close();
    }
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    return temp; }


    }
    package chuangsi_0424.io.serlizable;public class TestCopy { /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    Persion p=new Persion(19,"我是水",28,new Address("长沙","覃正平"));

    Persion p1=(Persion)p.getcopy();

    System.out.println(p1==p);
    System.out.println(p1.getAddress()==p.getAddress());
    }}