Redian新闻
>
a simple java programming question
avatar
a simple java programming question# Java - 爪哇娇娃
w*t
1
ok, I have a following bean class:
public class user {
String a1;
String a2;
....
String a20
setA1(){}
getA1(){}
...
setA20(){}
getA20(){}
}
I have a hashtable like following
key value
avatar
g*s
2
java.lang.Class
java.lang.reflect.Method

【在 w******t 的大作中提到】
: ok, I have a following bean class:
: public class user {
: String a1;
: String a2;
: ....
: String a20
: setA1(){}
: getA1(){}
: ...
: setA20(){}

avatar
w*t
3
show me how. Reflection is something I know. Thanks

【在 g*s 的大作中提到】
: java.lang.Class
: java.lang.reflect.Method

avatar
g*s
4
Hashtable ht = ....;
user userObject = ....;
for (Enumeration e = ht.keys() ; e.hasMoreElements() ;) {
String key = (String)e.nextElement();
String value = (String)ht.get(key);
Object [] methodParamValues = new Object[1];
methodParamValues[0] = value;
Method method = userObject.getClass().getMethod("set"+key,
new Class [] { new String().getClass() });
method.invoke(userObject, methodParamValues);
}

【在 w******t 的大作中提到】
: show me how. Reflection is something I know. Thanks
avatar
w*t
5
thanks.
It's not gong to work for my case though - in my real case, key and method
does not match that well. (key is string, it's from database table value, may
contain space)
I solved this issue other way.

【在 g*s 的大作中提到】
: Hashtable ht = ....;
: user userObject = ....;
: for (Enumeration e = ht.keys() ; e.hasMoreElements() ;) {
: String key = (String)e.nextElement();
: String value = (String)ht.get(key);
: Object [] methodParamValues = new Object[1];
: methodParamValues[0] = value;
: Method method = userObject.getClass().getMethod("set"+key,
: new Class [] { new String().getClass() });
: method.invoke(userObject, methodParamValues);

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。