java如何利用反射机制得到一个类中为数组类型的成员变量的值
写了个例子,请仔细查看代码,具体过程需要你自己对照java API 再研究,
先定义了一个普通的学生类,如下:
public class Student {
private int id;
private String name;
private int age;
private String[] aiHao;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String[] getAiHao() {
return aiHao;
}
public void setAiHao(String[] aiHao) {
this.aiHao = aiHao;
}
}
对学生类进行反射
public class Test3 {
public static void main(String[] args) {
Student s = new Student();
s.setId(1);
s.setName("张三");
s.setAge(23);
s.setAiHao(new String[] { "足球", "篮球", "上网" });
try {
// 获取所有的变量
Field[] fields = Student.class.getDeclaredFields();
// 循环处理变量
for (Field f : fields) {
f.setAccessible(true);
Object value = f.get(s); // 取变量的值
System.out.print("变量名称为:" + f.getName());
if (value.getClass().isArray()) { // 判断是否是数组
Object[] arr = (Object[]) value; // 装换成数组
System.out.println(",变量值等于:" + Arrays.toString(arr));
// 这里还可以做额外处理,判断数组的类型 如下面注释的代码
/*
for (Object a : arr) {
System.out.println("参数类型" + a.getClass().getName());
// 同样运用反射的机制做额外处理
}
*/
} else {
// 非数组类型,普通处理
System.out.println(",变量值等于:" + value);
}
f.setAccessible(false);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package cn.baayong.thread;
import java.lang.reflect.Method;
// 2014-2-26 下午02:18:17 created by haobo56
public class Test {
public static void main(String[] args) {
//构造对象
User user = new User();
String strs[] ={"1","2","3"};
user.setStrs(strs);
Method[] methods = user.getClass().getMethods();
for(Method m:methods){
String methodNames= m.getName();
if(methodNames.equals("getStrs")){
try {
Object obj =m.invoke(user, new Object[]{});
if(obj instanceof java.lang.String []){
String tempstrs [] =(String[]) obj;
for(String str:tempstrs){
System.out.println(str);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
class User{
private String strs[];
public String[] getStrs() {
return strs;
}
public void setStrs(String[] strs) {
this.strs = strs;
}
}
希望你采纳。 谢谢。
package com.cs.gang;
import java.lang.reflect.Field;
/**
* Created by IntelliJ IDEA.
* User: gang-liu
* Date: 2/26/14
* Time: 2:12 PM
*/
public class ReflectTest {
private String[] strData;
private int[] intData;
public ReflectTest() {
strData = new String[]{"one", "two", "three"};
intData = new int[]{1, 2, 4, 5, 6};
}
public ReflectTest(String[] strData, int[] intData) {
this.strData = strData;
this.intData = intData;
}
public String[] getStrData() {
return strData;
}
public void setStrData(String[] strData) {
this.strData = strData;
}
public int[] getIntData() {
return intData;
}
public void setIntData(int[] intData) {
this.intData = intData;
}
public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
final ReflectTest test = new ReflectTest();
final Field strField = ReflectTest.class.getDeclaredField("strData");
strField.setAccessible(true);
final String[] data = (String[]) strField.get(test);
for(final String d : data){
System.out.println(d);
}
final Field intField = ReflectTest.class.getDeclaredField("intData");
intField.setAccessible(true);
final int[] intDatas = (int[]) intField.get(test);
for(final int intData : intDatas){
System.out.println(intData);
}
}
}
array.setAccessible(true);
int[] arr=(int[])array.get(heap2);
System.out.println(Array.get(arr,2));//读取下标2数组元素
Array.set(arr,2,0);//修改下标2数组元素
array.set(heap2,arr);//赋值给对象heap2成员变量array
array.set(heap2,null);//置对象heap2成员变量数组为null