通过反射得到一个类的对象,如何对该对象对应的类的父类中的变量赋值? 继承类反射getDeclaredFields() 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 刚刚又看到一个方法,getFields,本以为能把集成的变量都找出来,结果不好使 Person person = new Person(10, "LL"); try { Field field = person.getClass().getDeclaredField("name"); field.setAccessible(true); field.set(person, "ERWA"); System.out.println(person.getName()); } catch (NoSuchFieldException | IllegalAccessException e) { e.printStackTrace(); }要什么就反射取吧 我的意思是SuperMan的实例,想通过反射得到SuperMan父类Person的变量 getdeclaredfields应该是不行的, 它好像只能得到当前类所有属性getfields按理应该可以获取到当前类极其父类的所有属性, 当然前提是属性得是public的 SuperMan superMan = new SuperMan(10, "LL"); try { //Field field = person.getClass().getDeclaredField("name"); Field field = superMan.getClass().getSuperclass().getDeclaredField("name"); field.setAccessible(true); field.set(superMan, "ERWA"); System.out.println(superMan.getName()); } catch (NoSuchFieldException | IllegalAccessException e) { e.printStackTrace(); }可以取到自己,再取个父类不就 OK? public static void main(String[] args) throws Exception { Class<?> clazz = null; clazz = Class.forName("bianchengzhimei.SuperMan"); Object p1 = clazz.newInstance(); Field personNameField = null; for(;clazz != Object.class;clazz= clazz.getSuperclass()){ try { personNameField = clazz.getDeclaredField("name"); } catch (Exception e) {} } personNameField.setAccessible(true); personNameField.set(p1, "darlingFish"); System.out.println(personNameField.get(p1)); } 7楼的方法基本上可以用了。另外推荐一种更规范的写法:Class<?> clazz = null; clazz = Class.forName("bianchengzhimei.SuperMan"); //使用符合JavaBean规范的属性访问器 PropertyDescriptor pd = new PropertyDescriptor("name", clazz); //调用setter Method writeMethod = pd.getWriteMethod(); //setName() writeMethod.invoke(obj, "test"); //调用getter Method readMethod = pd.getReadMethod(); //getName() readMethod.invoke(obj); private是私有变量,如果想给子类用,你就应该定义成protected啊。否则要这么多访问限定关键字干什么 循环终结在接循环 java 如何读取地磅称量数据 java串口程序出了个错误,我不知道哪里有错?请高手指点下,谢谢~~ C++调用java程序出错 各位大侠 一个莫名其妙的一场,在线等。 如何让JAVA跑得更快?请大家提点心得。 /** 乱码, 怎么搞定? 急!! */ 關於抽象類和接口的問題(大討論) SCJP的试题,请回答为什么和答案 jxl不去读取已经读取过的数据 java不能覆盖方法,正在尝试指定更低的访问权限
try {
Field field = person.getClass().getDeclaredField("name");
field.setAccessible(true); field.set(person, "ERWA");
System.out.println(person.getName());
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}要什么就反射取吧
我的意思是SuperMan的实例,想通过反射得到SuperMan父类Person的变量
getfields按理应该可以获取到当前类极其父类的所有属性, 当然前提是属性得是public的
try {
//Field field = person.getClass().getDeclaredField("name");
Field field = superMan.getClass().getSuperclass().getDeclaredField("name");
field.setAccessible(true);
field.set(superMan, "ERWA");
System.out.println(superMan.getName());
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}可以取到自己,再取个父类不就 OK?
Class<?> clazz = null;
clazz = Class.forName("bianchengzhimei.SuperMan");
Object p1 = clazz.newInstance();
Field personNameField = null;
for(;clazz != Object.class;clazz= clazz.getSuperclass()){
try {
personNameField = clazz.getDeclaredField("name");
} catch (Exception e) {}
}
personNameField.setAccessible(true);
personNameField.set(p1, "darlingFish"); System.out.println(personNameField.get(p1));
}
clazz = Class.forName("bianchengzhimei.SuperMan");
//使用符合JavaBean规范的属性访问器
PropertyDescriptor pd = new PropertyDescriptor("name", clazz);
//调用setter
Method writeMethod = pd.getWriteMethod(); //setName()
writeMethod.invoke(obj, "test");
//调用getter
Method readMethod = pd.getReadMethod(); //getName()
readMethod.invoke(obj);
否则要这么多访问限定关键字干什么