利用反射直接访问类中的私有变量
2012-01-02 22:41
417 查看
package reflection;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class testMain {
/**
* @param args
*/
public static void main(String[] args) {
ClassLoader loader = testMain.class.getClassLoader();
try {
Class c = loader.loadClass("reflection.Empolyee");
//Empolyee类中包含私有变量name,默认值为a;包含getName()方法
Empolyee e = (Empolyee)c.newInstance();
Field[] fields = c.getDeclaredFields();
//获取名为name的属性
Field f = c.getDeclaredField("name");
//取消访问权限检查
f.setAccessible(true);
//将reflection.Empolyee类型的实例e中的name属性值设为123
f.set(e, "123");
//获取名为getName的方法,没有参数
Method m = c.getMethod("getName", null);
//调用getName方法
System.out.println(m.invoke(e, null));
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class testMain {
/**
* @param args
*/
public static void main(String[] args) {
ClassLoader loader = testMain.class.getClassLoader();
try {
Class c = loader.loadClass("reflection.Empolyee");
//Empolyee类中包含私有变量name,默认值为a;包含getName()方法
Empolyee e = (Empolyee)c.newInstance();
Field[] fields = c.getDeclaredFields();
//获取名为name的属性
Field f = c.getDeclaredField("name");
//取消访问权限检查
f.setAccessible(true);
//将reflection.Empolyee类型的实例e中的name属性值设为123
f.set(e, "123");
//获取名为getName的方法,没有参数
Method m = c.getMethod("getName", null);
//调用getName方法
System.out.println(m.invoke(e, null));
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
相关文章推荐
- 【转】【Java】利用反射技术,实现对类的私有方法、变量访问
- 利用反射机制访问父类私有变量和属性
- 【原创】.NET中利用反射访问和修改类中的私有成员
- 论java之反射机制,访问类中私有变量,调用私有方法
- 利用java反射访问私有方法
- 黑马学习-------------反射(访问私有变量)
- C++在类的成员函数中,允许直接访问该类的成员对象的私有成员变量
- 利用反射调用私有方法、访问私有属性
- 利用反射读取类的私有变量
- Java中的反射机制,利用反射访问私有
- java利用反射访问类的私有成员
- .net 反射访问私有变量和私有方法
- 利用反射机制访问类中的私有方法
- 如何利用反射去访问一个私有的方法
- 类反射学习(三) JAVA如何利用类反射调用普通函数、访问成员成员变量
- 对象能够直接访问其私有成员变量
- 使用类的反射机制来访问类的私有成员变量
- 利用java反射机制,实现对类的私有变量和私有方法的访问
- C# 利用反射访问修改私有成员
- Java中的反射机制(四) 利用反射访问私有