由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - a simple java programming question
相关主题
simple java questionA question about inheritance
Java大侠们:Hashtable help please!Help: web service questions
问个hashtable实现问题[转载] Java 1.5 Generic 问题
折腾了一天,实在是绝望了,请教请教JAVA里什么METHOD是用来STRING PATTERN SEARCH
java StringTIJ上写错了?
一个Java程序员的话(3)overloading methods
how to copy an Object?怎样用class的string type name 动态生成object?
A simple questiona question on interface
相关话题的讨论汇总
话题: string话题: key话题: class话题: value
进入Java版参与讨论
1 (共1页)
w******t
发帖数: 1422
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
g*s
发帖数: 2277
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(){}

w******t
发帖数: 1422
3
show me how. Reflection is something I know. Thanks

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

g*s
发帖数: 2277
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
w******t
发帖数: 1422
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);

1 (共1页)
进入Java版参与讨论
相关主题
a question on interfacejava String
synchronized method does lock the object that passed into the method as a parameter?一个Java程序员的话(3)
Java练习题 12how to copy an Object?
Question: reflection and genericsA simple question
simple java questionA question about inheritance
Java大侠们:Hashtable help please!Help: web service questions
问个hashtable实现问题[转载] Java 1.5 Generic 问题
折腾了一天,实在是绝望了,请教请教JAVA里什么METHOD是用来STRING PATTERN SEARCH
相关话题的讨论汇总
话题: string话题: key话题: class话题: value