从超类启动声明的子类字段

时间:2015-10-09 06:36:28

标签: java android reflection

可以初始化字段的值吗?

我能够从超类

中获取子类中的字段
Field[] fields = this.getClass().getDeclaredFields();


for(Field field : fields) {
    //I need to assign a value to every field depending on their type

    if(Integer.class.isAssignableFrom(field.getType())) {
        //Something like this
        field = (Integer) 100;
    } else if(MyObject.class.isAssignableFrom(field.getType())) {
        //Something like this
        field = new MyObject();
    }
}

0 个答案:

没有答案