public static void main(String[] args) {
A a = new A();
Systemoutprintln(getValueInField(a,"b1","i"));
Systemoutprintln(getValueInField(a,"b2","i"));
Systemoutprintln(getValueInField(a,"b3","i"));
}
public static Object getValueInField(Object obj,String field,String name){
//三个参数分别是外部的类的对象obj,作为成员属性的类的引用名,要查询的类内部的属性名
try {
Object o = objgetClass()getDeclaredField(field)get(obj);
return ogetClass()getDeclaredField(name)get(o);
} catch (Exception e) {
Systemoutprintln("查找失败");
return null;
}
}
}
class A{
B b1 = new B(1);
B b2 = new B(2);
}
class B{
int i;
B(int i){
thisi = i;
}
}Intent不仅能传递java基本数据类型,还能传递类的对象,前提是这些类必须实现Serializable或者Parcelable接口。实现Serializable接口的方式是通过对象的序列化和反先迭代出里面的元素
然后对每个元素取属性值
List<Student> list
比如里面存的是Student
Student有一个属性name
Iterator it = listiterator();
while(ithasNext()){
Student stu = itnext();
String name = stugetName();//得到了里面的元素的属性了
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)