62,614
社区成员
发帖
与我相关
我的任务
分享
public bool insert(IEntity entity)
{
PropertyInfo[] pis = entity.GetType.GetProperties();
String a = pis[0].GetValue(entity, null);
}
public class Test implements IEntity{
private String s = "反射成功!";
public String getS(){
return s;
}
}
public interface IEntity{
}
public class DataBaseOperations{
public void test(IEntity ie)
{
//PropertyInfo[] pis = ie.
}
}
Test test = new Test();
PropertyDescriptor[] pds = Introspector.getBeanInfo(Test.class).getPropertyDescriptors();
for(PropertyDescriptor pd : pds){
if("s".equals(pd.getName())){
Method getter = pd.getReadMethod();
Object value = getter.invoke(test);
System.out.println(value);
}
}
package test;
import java.lang.reflect.Field;
interface IEntity{
}
class Entity implements IEntity{
private String s1 = "字符串1";
private String s2 = "字符串2";
}
public class Test {
public static void reflect(IEntity e) throws Exception{
Class cls = e.getClass();
Field[] fields = cls.getDeclaredFields();
for(int i=0; i<fields.length; i++){
Field f = fields[i];
f.setAccessible(true);
System.out.println("属性名:" + f.getName() + " 属性值:" + f.get(e));
}
}
public static void main(String[] args) throws Exception{
IEntity e = new Entity();
reflect(e);
}
}