您的位置:

java复制对象,java 复制对象,不影响原对象

本文目录一览:

Java中,复制一个对象,有什么好的方法

使用Java的反射机制实现:为了能更好的区分,写成了两个类,可以运行下面的代码看看效果

import java.lang.reflect.Field;

import java.lang.reflect.Method;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

public class Test {

public static void main(String[] args) throws Exception {

Customer1 c1 = new Customer1();

c1.setName("c1");

ListString list = new ArrayListString();

list.add("1");

list.add("2");

c1.setList(list);

MapString,String map = new HashMapString, String();

map.put("map1", "map1");

map.put("map2", "map2");

c1.setMap(map);

Customer2 c2 = new Customer2();

//

Class c = c1.getClass();

Class class2 = c2.getClass();

Field fields[] = c.getDeclaredFields();

for (int i = 0; i fields.length; i++) {

Field field = fields[i];

String fieldName = field.getName();

String firstLetter = fieldName.substring(0, 1).toUpperCase();

String getMethodName = "get" + firstLetter + fieldName.substring(1);

String setMethodName = "set" + firstLetter + fieldName.substring(1);

Method getMethod = c.getMethod(getMethodName, new Class[] {});

Method setMethod = class2.getMethod(setMethodName,

new Class[] { field.getType() });

Object value = getMethod.invoke(c1, new Object[] {});

setMethod.invoke(c2, new Object[] { value });

}

System.out.println(c2.getName());

System.out.println(c2.getList());

System.out.println(c2.getMap());

}

}

class Customer1 {

private String name;

private ListString list;

private MapString, String map;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public ListString getList() {

return list;

}

public void setList(ListString list) {

this.list = list;

}

public MapString, String getMap() {

return map;

}

public void setMap(MapString, String map) {

this.map = map;

}

}

class Customer2 {

private String name;

private ListString list;

private MapString, String map;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public ListString getList() {

return list;

}

public void setList(ListString list) {

this.list = list;

}

public MapString, String getMap() {

return map;

}

public void setMap(MapString, String map) {

this.map = map;

}

}

java怎么样构造函数复制一个对象

java复制对象:

一、使用clone。对象可克隆的类必须实现Cloneable接口,并且clone方法是浅克隆。

二、类实现Serializable,用ObjectOutputStream、ObjectInputStream 来复制对象。

三、可以直接new一个对象,赋值!

如何复制一个java对象

/**

     * 复制对象

     *

     * @param srcObj

     * @return

     */

    public static Object depthClone(Object srcObj) {

        if (srcObj == null) {

            return null;

        }

        Object cloneObj = null;

        ByteArrayOutputStream out = null;

        ObjectOutputStream oo = null;

        ByteArrayInputStream in = null;

        ObjectInputStream oi = null;

        try {

            out = new ByteArrayOutputStream();

            oo = new ObjectOutputStream(out);

            oo.writeObject(srcObj);

            in = new ByteArrayInputStream(out.toByteArray());

            oi = new ObjectInputStream(in);

            cloneObj = oi.readObject();

        } catch (IOException e) {

            e.printStackTrace();

        } catch (ClassNotFoundException e) {

            e.printStackTrace();

        } finally {

            if (out != null) {

                try {

                    out.close();

                } catch (IOException ex) {

                    ex.printStackTrace();

                }

            }

            if (oo != null) {

                try {

                    oo.close();

                } catch (IOException ex) {

                    ex.printStackTrace();

                }

            }

            if (in != null) {

                try {

                    in.close();

                } catch (IOException ex) {

                    ex.printStackTrace();

                }

            }

            if (oi != null) {

                try {

                    oi.close();

                } catch (IOException ex) {

                    ex.printStackTrace();

                }

            }

        }

        return cloneObj;

    }