1. 程式人生 > >比較兩個對象屬性及屬性值

比較兩個對象屬性及屬性值

bject ace ive args main authorize code pre cer

import java.lang.reflect.Method;
private static Object getFieldValue(Object thisClass, String fieldName)  
       {  
           Object value = new Object();  
           Method method = null;  
           try {  
               String methodName = fieldName.substring(0, 1).toUpperCase()+ fieldName.substring(1);  
               method 
= thisClass.getClass().getMethod("get" + methodName); value = method.invoke(thisClass); } catch (Exception e) { e.printStackTrace(); } return value; }
public static void main(String args[]) {
        AuthServiceAuthorizeDO authserviceauthorizedo 
= new AuthServiceAuthorizeDO(); authserviceauthorizedo.setSv_gis_certified("1"); authserviceauthorizedo.setSv_gusername("2"); authserviceauthorizedo.setId("0"); YongHu yonghu = new YongHu(); yonghu.setUsername("bbbbb"); yonghu.setIs_certified("aaaaaaaaaaa"); yonghu.setId(
"ddddddddddd"); Field[] fields1 = authserviceauthorizedo.getClass().getDeclaredFields();//獲取屬性名稱數組 Field[] fields2 = yonghu.getClass().getDeclaredFields();//獲取屬性名稱數組 for (int i = 0; i < fields1.length; i++) { Object valueObj1 = getFieldValue(authserviceauthorizedo ,fields1[i].getName());//獲取屬性值 for (int j = 0; j < fields2.length; j++) { Object valueObj2 = getFieldValue(yonghu ,fields2[j].getName());//獲取屬性值 if(fields1[i].getName().equals("sv_g"+fields2[j].getName())){ switch (String.valueOf(valueObj1)) { case "0": System.out.println("---"); break; case "1": System.out.println(valueObj2); break; case "2": System.out.println(SensitiveInfoUtils.toConceal(String.valueOf(valueObj2))); break; } } } } }

比較兩個對象屬性及屬性值