public static <T> void testGetOrSet(List<T> list) throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{ Class tClass = list.get(0).getClass(); //得到該類的全部屬性 Field[] fields = tClass.getDeclaredFields(); for(Field field:fields){ PropertyDescriptor pd = new PropertyDescriptor(field.getName(), tClass); //得到set方法 Method method = pd.getWriteMethod(); method.invoke(list.get(0), new Object[]{"123"}); //得到get方法 Method get = pd.getReadMethod(); Object getValue = get.invoke(list.get(0), new Object[]{}); System.out.println("field:"+field.getName()+"---getValue:"+getValue); } }