Java基础知识之反射机制

1、什么是反射机制

Java的反射机制是在运行中的,可以通过反射机制获取(知道)一个类中的所有方法和属性,对于任意一个类,也可以通过反射机制得到(调用)一个对象中的所有方法和属性;这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制。

2、反射机制能做什么
反射机制主要提供了以下功能:

在运行时,判断任意一个对象,它所属的类是哪一个;

在运行时,可以构造(创建)任意一个类的对象;

在运行时,判断任意一个类,它所具有的成员变量和方法;

在运行时,可以调用任意一个对象的方法;

生成动态代理。

3、反射机制的相关方法

1.通过一个对象获得完整的包名和类名

package com.reflect;
public class TestReflect {
    public static void main(String[] args) throws Exception{<br>
        TestReflect testReflect = new TestReflect();
        System.out.println(testReflect.getClass().getName());// 结果 com.reflect.TestReflect
    }
}

2.实例化Class类对象

package com.reflect;
 
public class TestReflect {
 
    public static void main(String[] args) throws Exception{
 
        Class<?> class1 = null;
                Class<?> class2 = null;
                Class<?> class3 = null;
 
                // 一般采用这种形式(实例化Class类对象)
              class1 = Class.forName("com.reflect.TestReflect");
              class2 = new TestReflect().getClass();
              class3 = TestReflect.class;
 
              System.out.println("类名称  " + class1.getName());
              System.out.println("类名称  " + class2.getName());
              System.out.println("类名称  " + class3.getName());
              //三条的打印结果都是:类名称  com.reflect.TestReflect
    }   
}

3.获取一个对象的父类与实现的接口

package com.reflect;
 
import java.io.Serializable;
 
public class TestReflect implements Serializable {
 
 
    private static final long serialVersionUID = -2862585049955236662L;
    public static void main(String[] args) throws Exception {
            Class<?> clazz = Class.forName("com.reflect.TestReflect");
            // 取得父类
            Class<?> parentClass = clazz.getSuperclass();
            System.out.println("clazz的父类为:" + parentClass.getName());// clazz的父类为: java.lang.Object
 
            // 获取所有的接口
            Class<?> intes[] = clazz.getInterfaces();
            for (int i = 0; i < intes.length; i++) {
                System.out.println("clazz实现的接口有:"+(i + 1) + ":" + intes[i].getName());//clazz实现的接口有:1:java.io.Serializable
            }
    }}                   

4、获取某个类的全部属性

package com.reflect;
 
import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
 
public class TestReflect implements Serializable {
 
 
       private static final long serialVersionUID = -2862585049955236662L;
        public static void main(String[] args) throws Exception {
            Class<?> clazz = Class.forName("com.reflect.TestReflect");
            // 取得本类的全部属性
            Field[] field = clazz.getDeclaredFields();
            for (int i = 0; i < field.length; i++) {
                // 权限修饰符
                int mo = field[i].getModifiers(); //==26
                String priv = Modifier.toString(mo); //==private static final
                // 属性类型
                Class<?> type = field[i].getType();
                System.out.println(priv + "----- " + type.getName() + " ----" + field[i].getName());              //==private static final----- long ----serialVersionUID;
            }
            // 取得实现的接口或者父类的属性
            Field[] filed1 = clazz.getFields();
            for (int j = 0; j < filed1.length; j++) {
                // 权限修饰符
                int mo = filed1[j].getModifiers();
                String priv = Modifier.toString(mo);
                // 属性类型
                Class<?> type = filed1[j].getType();
                System.out.println(priv + " " + type.getName() + " " + filed1[j].getName() + ";");
            }
        }
    }
   

5、获取某个类的全部方法

package com.reflect;
 
import java.io.Serializable;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
 
 
/**
 * java的反射机制
 * @author hejinjin
 *
 */
public class TestReflect implements Serializable {
 
 
        private static final long serialVersionUID = -2862585049955236662L;
        public static void main(String[] args) throws Exception {
            Class<?> clazz = Class.forName("com.reflect.TestReflect");
            Method method[] = clazz.getMethods();
            for (int i = 0; i < method.length; ++i) {
                Class<?> returnType = method[i].getReturnType();
                Class<?> para[] = method[i].getParameterTypes();
                int temp = method[i].getModifiers();
                System.out.print(Modifier.toString(temp) + " ");
                System.out.print(returnType.getName() + "  ");
                System.out.print(method[i].getName() + " ");
                System.out.print("(");
                for (int j = 0; j < para.length; ++j) {
                    System.out.print(para[j].getName() + " " + "arg" + j);
                    if (j < para.length - 1) {
                        System.out.print(",");
                    }
                }
                Class<?> exce[] = method[i].getExceptionTypes();
                if (exce.length > 0) {
                    System.out.print(") throws ");
                    for (int k = 0; k < exce.length; ++k) {
                        System.out.print(exce[k].getName() + " ");
                        if (k < exce.length - 1) {
                            System.out.print(",");
                        }
                    }
                } else {
                    System.out.print(")");
                }
                System.out.println();
            }
        }
    }

猜你喜欢

转载自www.linuxidc.com/Linux/2017-07/145671.htm