「java对象copy」Java对象序列化

博主:adminadmin 2022-11-25 07:58:06 52

今天给各位分享java对象copy的知识,其中也会对Java对象序列化进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java对象值的copy

让B成为A的子类,使用JAVA的反射机制,自己写一个子类拷贝父类的属性的函数,这样无论父类有几个属性都可以不用修改代码了。

参考一下:

java 如何复制一个对象?

给你个小例子

class test implents Cloneable

{

test t1 = new test();

test t2 = null;

try {

t2 = (test)t1.clone();

} catch (CloneNotSupportedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

用clone()方法,不过在对象重载了Object的clone才能用。Object的clone方法是protected。

java 代码实现对象的拷贝

package com.teven.comefromnet;

/**

* Name:C.java

* Founction:

* Created:2011-9-15

* @author :teven

*/

public class CloneTest {

static class A implements Cloneable{

int i=1;

@Override

protected A clone() throws CloneNotSupportedException {

return (A)super.clone();

}

}

static class B extends A{

int j=2;

@Override

protected B clone() throws CloneNotSupportedException {

return (B)super.clone();

}

}

static class C extends B{

int k=3;

@Override

protected C clone() throws CloneNotSupportedException {

return (C)super.clone();

}

}

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

A a = new CloneTest.A();

A aa = (A) a.clone();

System.out.println("before clone a.i = "+ a.i+" after clone aa.i=" +aa.i);

B b = new CloneTest.B();

B bb= (B) b.clone();

System.out.println("before clone b.j = "+ b.j+" after clone bb.j=" +bb.j);

C c = new CloneTest.C();

C cc= (C) c.clone();

System.out.println("before clone c.k = "+ c.k+" after clone cc.k=" +cc.k);

}

}

如何复制一个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;

    }

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对象copy的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于Java对象序列化、java对象copy的信息别忘了在本站进行查找喔。

The End

发布于:2022-11-25,除非注明,否则均为首码项目网原创文章,转载请注明出处。