android List<?>泛型传递不同的model,并以反射获取model中属性名与属性值

 

通过List<?>传递不同的model,Field获取Model中属性名属性值

@RequiresApi(api = Build.VERSION_CODES.N)
    public static void  test(List<?> list){
        AtomicInteger intLength= new AtomicInteger();

        AtomicReference<String> name1= new AtomicReference<>("");
        AtomicReference<String> name2= new AtomicReference<>("");
        AtomicReference<String> name3= new AtomicReference<>("");
        AtomicReference<String> name4= new AtomicReference<>("");
        AtomicReference<String> name5= new AtomicReference<>("");
      //  final Object[] obj = {null};
        list.forEach((e)->{
            for (Field field:e.getClass().getDeclaredFields()){
                field.setAccessible(true);
                name1.set(field.getName());
                try {
                     name2.set(field.get(e).toString());
                } catch (IllegalAccessException illegalAccessException) {
                    illegalAccessException.printStackTrace();
                }

            }
        });

    }

 

posted @ 2021-08-19 09:33  盛沧海  阅读(242)  评论(0)    收藏  举报