This article is a detailed analysis and introduction to java object replication. Friends who need it can refer to it
Java itself provides the ability to copy objects. There is a clone method in the java.lang.Object class. This method is a protected method. Subclasses need to override this method and declare it as a public type. They also need to implement the Cloneable interface to provide the ability to copy objects. clone() is a native method. Generally speaking, the efficiency of native methods is It is much higher than the non-native method in Java. If you are more concerned about performance, consider this method first. There are many examples of this kind of copying on the Internet, so I won’t go into detail here. Another method to be used here is through Java. The reflection mechanism copies objects. This method may be less efficient than clone(), and does not support deep copying and copying collection types, but its versatility will be much improved. The following is the code for copying:
The code is as follows:
private <T> T getBean(T TargetBean, T SourceBean) { if (TargetBean== null) return null; Field[] tFields = TargetBean.getClass().getDeclaredFields(); Field[] sFields = SourceBean.getClass().getDeclaredFields(); try { for (Field field : tFields ) { String fieldName = field.getName(); if (fieldName.equals("serialVersionUID")) continue; if (field.getType() == Map.class) continue; if (field.getType() == Set.class) continue; if (field.getType() == List.class) continue; for (Field sField : sFields) { if(!sField .getName().equals(fieldName)){ continue; } Class type = field.getType(); String setName = getSetMethodName(fieldName); Method tMethod = TargetBean.getClass().getMethod(setName, new Class[]{type}); String getName = getGetMethodName(fieldName); Method sMethod = SourceBean.getClass().getMethod(getName, null); Object setterValue = voMethod.invoke(SourceBean, null); tMethod.invoke(TargetBean, new Object[]{setterValue}); } } } catch (Exception e) { throw new Exception("设置参数信息发生异常", e); } return TargetBean;}
This method receives two parameters, one is the source object to be copied - the object to be copied, and the other is the target object to be copied - Object copy. Of course, this method can also be used between two different objects. At this time, as long as the target object and the object have one or more attributes of the same type and name, the attribute values of the source object will be assigned to the attributes of the target object. .
The above is the detailed content of Detailed explanation of examples of java object copying. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version
Visual web development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.