首页 > 编程知识 正文

java对象复制到另一个对象,java清空对象属性值

时间:2023-05-04 02:18:16 阅读:166368 作者:311

使用JAVA反射机制移除对象中字符串属性的空格。

注:如果属性本身为“”,也会被替换。 需要过滤的情况下,object!=null之后追加判断即可

publicstaticvoidobjecttotrim (objectobj ) throws IllegalAccessException {

映射图=new hashmap (;

field [ ] declared fields=obj.getclass ().getDeclaredFields;

场域:取消场域(for ) {

String type=field.getType ().getCanonicalName );

if (string utils.equals (Java.lang.string ),type ) ) ) ) ) ) ) )。

Field.setaccessible(true;

objectobject=field.get(obj;

对象!=null ) {

String trim=object.toString ().replace (',' );

map.put(field.getname )、trim );

}

}

}

场域:取消场域(for ) {

if(map.get(field.getname ) )!=null ) {

strings=map.get(field.getname ();

Field.setaccessible(true;

Field.set(obj,s );

}

}

}

publicstaticvoidmain (string [ ] args ) throws IllegalAccessException { )。

IdCode idCode=new IdCode (;

idcode.setdeleteflag(1;

idcode.set create date (local datetime.now );

idCode.setDeptId (' );

idcode.seteducationurl(abvc );

idcode.setidcode(bcDDD );

system.out.println (idcode.tostring () );

system.out.print ln ((==========抽出了空格==========) )

objecttotrim(idcode;

system.out.println (idcode.tostring () );

}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。