java來實現 比如有一個 int arr[] = {23,456,6567,3456,234}有一個User類,有屬性 id, name,password,sex,tel,怎么把上述數組依次賦值給一個user類呢,就是id=23,name=456,等等,該如何來實現 呢
1 回答

慕容森
TA貢獻1853條經驗 獲得超18個贊
public static <T> T setParam(Class<T> clazz, Object[] args) throws Exception { if (clazz == null || args == null) { throw new IllegalArgumentException(); } T t = clazz.newInstance(); Field[] fields = clazz.getDeclaredFields(); if (fields == null || fields.length > args.length) { throw new IndexOutOfBoundsException(); } for (int i = 0; i < fields.length; i++) { fields[i].setAccessible(true); fields[i].set(t, args[i]); } return t; }
添加回答
舉報
0/150
提交
取消