通俗地说,反射机制就是可以把一个类,类的成员(函数,属性),当成一个对象来操作,希望读者能理解,也就是说,类,类的成员,我们在运行的时候还可以动态地去操作他们.
理论的东东太多也没用,下面我们看看实践 Demo ~
Demo:
- package cn.lee.demo;
-
- import java.lang.reflect.Constructor;
- import java.lang.reflect.Field;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.lang.reflect.Modifier;
- import java.lang.reflect.TypeVariable;
-
- public class Main {
-
-
-
-
-
-
-
-
-
-
-
-
- public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, SecurityException, NoSuchFieldException, NoSuchMethodException {
-
-
-
- Demo1();
- System.out.println("===============================================");
-
-
- Demo2();
- System.out.println("===============================================");
-
-
- Demo3();
- System.out.println("===============================================");
-
-
- Demo4();
- System.out.println("===============================================");
-
-
- Demo5();
- System.out.println("===============================================");
-
-
- Demo6();
- System.out.println("===============================================");
-
-
- Demo7();
- System.out.println("===============================================");
-
-
- Demo8();
- System.out.println("===============================================");
-
- }
-
-
-
-
- public static void Demo1()
- {
- Person person = new Person();
- System.out.println("Demo1: 包名: " + person.getClass().getPackage().getName() + ","
- + "完整类名: " + person.getClass().getName());
- }
-
-
-
-
-
- public static void Demo2() throws ClassNotFoundException
- {
-
- Class<?> class1 = null;
- Class<?> class2 = null;
-
-
- class1 = Class.forName("cn.lee.demo.Person");
- System.out.println("Demo2:(写法1) 包名: " + class1.getPackage().getName() + ","
- + "完整类名: " + class1.getName());
-
-
- class2 = Person.class;
- System.out.println("Demo2:(写法2) 包名: " + class2.getPackage().getName() + ","
- + "完整类名: " + class2.getName());
- }
-
-
-
-
-
-
-
- public static void Demo3() throws ClassNotFoundException, InstantiationException, IllegalAccessException
- {
- Class<?> class1 = null;
- class1 = Class.forName("cn.lee.demo.Person");
-
- Person person = (Person) class1.newInstance();
- person.setAge(20);
- person.setName("LeeFeng");
- System.out.println("Demo3: " + person.getName() + " : " + person.getAge());
- }
-
-
-
-
-
-
-
-
-
- public static void Demo4() throws ClassNotFoundException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException
- {
- Class<?> class1 = null;
- Person person1 = null;
- Person person2 = null;
-
- class1 = Class.forName("cn.lee.demo.Person");
-
- Constructor<?>[] constructors = class1.getConstructors();
-
- person1 = (Person) constructors[0].newInstance();
- person1.setAge(30);
- person1.setName("leeFeng");
-
- person2 = (Person) constructors[1].newInstance(20,"leeFeng");
-
- System.out.println("Demo4: " + person1.getName() + " : " + person1.getAge()
- + " , " + person2.getName() + " : " + person2.getAge()
- );
-
- }
-
-
-
-
-
-
-
-
-
-
-
- public static void Demo5() throws IllegalArgumentException, IllegalAccessException, SecurityException, NoSuchFieldException, InstantiationException, ClassNotFoundException
- {
- Class<?> class1 = null;
- class1 = Class.forName("cn.lee.demo.Person");
- Object obj = class1.newInstance();
-
- Field personNameField = class1.getDeclaredField("name");
- personNameField.setAccessible(true);
- personNameField.set(obj, "胖虎先森");
-
-
- System.out.println("Demo5: 修改属性之后得到属性变量的值:" + personNameField.get(obj));
-
- }
-
-
-
-
-
-
- public static void Demo6() throws ClassNotFoundException
- {
- Class<?> class1 = null;
- class1 = Class.forName("cn.lee.demo.SuperMan");
-
-
- Class<?> superClass = class1.getSuperclass();
- System.out.println("Demo6: SuperMan类的父类名: " + superClass.getName());
-
- System.out.println("===============================================");
-
-
- Field[] fields = class1.getDeclaredFields();
- for (int i = 0; i < fields.length; i++) {
- System.out.println("类中的成员: " + fields[i]);
- }
- System.out.println("===============================================");
-
-
-
- Method[] methods = class1.getDeclaredMethods();
- for (int i = 0; i < methods.length; i++) {
- System.out.println("Demo6,取得SuperMan类的方法:");
- System.out.println("函数名:" + methods[i].getName());
- System.out.println("函数返回类型:" + methods[i].getReturnType());
- System.out.println("函数访问修饰符:" + Modifier.toString(methods[i].getModifiers()));
- System.out.println("函数代码写法: " + methods[i]);
- }
-
- System.out.println("===============================================");
-
-
- Class<?> interfaces[] = class1.getInterfaces();
- for (int i = 0; i < interfaces.length; i++) {
- System.out.println("实现的接口类名: " + interfaces[i].getName() );
- }
-
- }
-
-
-
-
-
-
-
-
-
-
-
- public static void Demo7() throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException
- {
- Class<?> class1 = null;
- class1 = Class.forName("cn.lee.demo.SuperMan");
-
- System.out.println("Demo7: \n调用无参方法fly():");
- Method method = class1.getMethod("fly");
- method.invoke(class1.newInstance());
-
- System.out.println("调用有参方法walk(int m):");
- method = class1.getMethod("walk",int.class);
- method.invoke(class1.newInstance(),100);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static void Demo8() throws ClassNotFoundException
- {
- Class<?> class1 = null;
- class1 = Class.forName("cn.lee.demo.SuperMan");
- String nameString = class1.getClassLoader().getClass().getName();
-
- System.out.println("Demo8: 类加载器类名: " + nameString);
- }
-
-
-
- }
-
-
-
-
-
- class Person{
- private int age;
- private String name;
- public Person(){
-
- }
- public Person(int age, String name){
- this.age = age;
- this.name = name;
- }
-
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }
-
- class SuperMan extends Person implements ActionInterface
- {
- private boolean BlueBriefs;
-
- public void fly()
- {
- System.out.println("超人会飞耶~~");
- }
-
- public boolean isBlueBriefs() {
- return BlueBriefs;
- }
- public void setBlueBriefs(boolean blueBriefs) {
- BlueBriefs = blueBriefs;
- }
-
- @Override
- public void walk(int m) {
-
- System.out.println("超人会走耶~~走了" + m + "米就走不动了!");
- }
- }
- interface ActionInterface{
- public void walk(int m);
- }
个人觉得使用反射机制的一些地方:
1.工厂模式:Factory类中用反射的话,添加了一个新的类之后,就不需要再修改工厂类Factory了
2.数据库JDBC中通过Class.forName(Driver).来获得数据库连接驱动
3.分析类文件:毕竟能得到类中的方法等等
4.访问一些不能访问的变量或属性:破解别人代码
Demo下载地址:http://download.csdn.net/detail/u011133213/6420969
反射的用途 Uses of Reflection
Reflection is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique and can enable applications to perform operations which would otherwise be impossible.
反射被广泛地用于那些需要在运行时检测或修改程序行为的程序中。这是一个相对高级的特性,只有那些语言基础非常扎实的开发者才应该使用它。如果能把这句警示时刻放在心里,那么反射机制就会成为一项强大的技术,可以让应用程序做一些几乎不可能做到的事情。
反射的缺点 Drawbacks of Reflection
Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection.
尽管反射非常强大,但也不能滥用。如果一个功能可以不用反射完成,那么最好就不用。在我们使用反射技术时,下面几条内容应该牢记于心:
性能第一
Performance Overhead
Because reflection involves types that are dynamically resolved, certain Java virtual machine optimizations can not be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts, and should be avoided in sections of code which are called frequently in performance-sensitive applications.
反射包括了一些动态类型,所以JVM无法对这些代码进行优化。因此,反射操作的效率要比那些非反射操作低得多。我们应该避免在经常被 执行的代码或对性能要求很高的程序中使用反射。
安全限制
Security Restrictions
Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet.
使用反射技术要求程序必须在一个没有安全限制的环境中运行。如果一个程序必须在有安全限制的环境中运行,如Applet,那么这就是个问题了。。
内部暴露
Exposure of Internals
Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing
private
fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.
由于反射允许代码执行一些在正常情况下不被允许的操作(比如访问私有的属性和方法),所以使用反射可能会导致意料之外的副作用--代码有功能上的错误,降低可移植性。反射代码破坏了抽象性,因此当平台发生改变的时候,代码的行为就有可能也随着变化。