了解泛型、反射、单例模式的请进。

codeartisan codeartisan 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
先贴上写好了的代码,问题在代码后面。
是一个简单的BeanUtil,具体的功能是复制VO的属性值到PO,如果VO的属性值为null则不复制。

package net.javayuan.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
* @(#)VO2PO.java Jan 23, 2008 9:00:55 AM
*
* @author Yuan
* VO到PO的属性复制工具
*/
public class VO2PO {

private static final VO2PO INSTANCE = new VO2PO();

private VO2PO(){
//
}

public static VO2PO getInstance(){
return INSTANCE;
}

/**
* 复制属性
* @param vo 源对象
* @param po 目标对象
* */
public void copyProperties(Object vo, Object po){

Class<?> voClass = vo.getClass();
Class<?> poClass = po.getClass();

Method[] methods = voClass.getDeclaredMethods();

for (Method method : methods){
String methodName = method.getName();
String fieldName = "";
String setterName = "";

if (methodName.startsWith("get")){
fieldName = methodName.substring(3,4).toLowerCase() + methodName.substring(4);
setterName = "set" + methodName.substring(3);
}

if (methodName.startsWith("is")){
fieldName = methodName.substring(2,3).toLowerCase() + methodName.substring(3);
setterName = "set" + methodName.substring(2);
}

if (!"".equals(fieldName)){
try {
Object o = method.invoke(vo, new Object[]{});
if (o != null){
Class<?> fieldType = poClass.getDeclaredField(fieldName).getType();
Method setter = poClass.getDeclaredMethod(setterName, fieldType);
setter.invoke(po, o);
}
} catch (IllegalArgumentException ex) {
ex.printStackTrace();
} catch (SecurityException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (InvocationTargetException ex) {
ex.printStackTrace();
} catch (NoSuchMethodException ex) {
ex.printStackTrace();
} catch (NoSuchFieldException ex) {
ex.printStackTrace();
}
}
}
}
public static void main(String[] args){
Bean bean1 = new Bean();
bean1.setBooleanValue(true);
bean1.setStringValue(null);
bean1.setIntValue(321);

Bean bean2 = new Bean();
bean2.setBooleanValue(false);
bean2.setStringValue("bean2");
bean2.setIntValue(123);

VO2PO.getInstance().copyProperties(bean1, bean2);

System.out.println(bean2.getIntValue());
System.out.println(bean2.getStringValue());
System.out.println(bean2.isBooleanValue());
}
}

class Bean{

private int intValue;
private String stringValue;
private boolean booleanValue;

public int getIntValue() {
return intValue;
}

public void setIntValue(int intValue) {
this.intValue = intValue;
}

public String getStringValue() {
return stringValue;
}

public void setStringValue(String stringValue) {
this.stringValue = stringValue;
}

public boolean isBooleanValue() {
return booleanValue;
}

public void setBooleanValue(boolean booleanValue) {
this.booleanValue = booleanValue;
}
}


第一个问题:是否所有用到Object做参数的地方都可以(或者有必要)写成泛型?
第二个问题:如果第一个问题的答案为true,我想把这个VO2PO类改写成泛型的VO2PO <V,P> ,该怎么改?
第三个问题:类似这样的工具类,你一般是如何设计?单例?静态方法类?还是一般的类?为什么?
...全文

相关推荐

评论 8
收藏 已收藏
分享
返回顶部
codeartisan
Java SE 62,621 成员 307,251 内容