- import java.beans.PropertyDescriptor;
- import java.lang.reflect.Method;
- import java.lang.*;
- class Test
- {
- public static void main(String[] args) throws Exception
- {
- cc obj=new cc("eulav");
- setProperty(obj,"propertyName","value");
- System.out.println(obj.getPropertyName());
- }
- public static void setProperty(Object obj,String propertyName,Object value)throws Exception
- {
- PropertyDescriptor pd=new PropertyDescriptor(propertyName,obj.getClass());
- Method method=pd.getWriteMethod();
- method.invoke(obj,value);
-
- }
- }
- class cc
- {
- private String propertyName;
- cc(String propertyName)
- {
- this.propertyName=propertyName;
- }
- public String getPropertyName()
- {
- return propertyName;
- }
- public void setPropertyName(String propertyName)
- {
- this.propertyName=propertyName;
- }
- }
复制代码
刚好看到在 求指教
|