2016.10.12-java 反射机制的初探

2016-10-12  本文已影响1人  LH_0811

首先声明两个类

Word

public class Word {
    private boolean empty;
    public String context;
    public static void welcome() {
        System.out.println("欢迎使用word");
    }
    public void open(){
        System.out.println("打开word文档");
    }
    public boolean save(String text){
        System.out.println("保存word文档:"+text);
        return true;
    }
    public boolean save(int int1,String text1){
        System.out.println("保存word文档:"+int1+" and "+text1);
        return true;
    }
}

Excel

public class Excel {
    public static void welcome() {
        System.out.println("欢迎使用excel");
    }
    public void open(){
        System.out.println("打开excel文档");
    }
    public boolean save(String text){
        System.out.println("保存excel文档");
        return true;
    }
    public boolean save(int int1,String text1){
        System.out.println("保存excel文档:"+int1+" and "+text1);
        return true;
    }
}

创建一个Test方法:做如下测试
#######1.动态获取一个类的信息;包括公共的属性以及属性的类型,自定义的属性以及类型,公共方法的返回值 参数列表以及参数属性,自定义方法的返回值 参数列表以及参数属性,

/**
     * 获得类的信息,包括方法 和 属性
     * */
    private static void getClassInfo (String className) {
        try {
            //动态加载类
            Class oa = Class.forName(className);
            // 获得所有的方法
            Method[] methods = oa.getMethods();
            System.out.println("=====getMethods=====");
            for (Method method : methods) {
                System.out.print(method.getReturnType().getSimpleName() + " ");
                System.out.print(method.getName());
                System.out.print("[");
                Parameter[] params = method.getParameters();
                for (Parameter parameter : params) {
                    System.out.print(parameter.getType().getSimpleName() + " ");
                    System.out.print(parameter.getName() + ",");
                }
                System.out.println("]");
            }
            // 获得 自己定义的方法列表
            Method[] declaredMethods = oa.getDeclaredMethods();
            System.out.println("=====getDeclaredMethods=====");
            for (Method method : declaredMethods) {
                System.out.print(method.getReturnType().getSimpleName() + " ");
                System.out.print(method.getName());
                System.out.print("[");
                Parameter[] params = method.getParameters();
                for (Parameter parameter : params) {
                    System.out.print(parameter.getType().getSimpleName() + " ");
                    System.out.print(parameter.getName() + ",");
                }
                System.out.println("]");
            }
            //获得公共属性
            Field[] fields = oa.getFields();
            System.out.println("=====getFields=====");
            for (Field field : fields) {
                System.out.print(field.getType().getSimpleName()+":");
                System.out.println(field.getName()); 
            }
            //获得自定义属性
            Field[] decFields = oa.getDeclaredFields();
            System.out.println("=====getDeclaredFields=====");
            for (Field field : decFields) {
                System.out.print(field.getType().getSimpleName()+":");
                System.out.println(field.getName()); 
            }
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

#######1.动态获得类的方法,并调用他们

/**
     * 类方法的动态调用
     * */
    private static void invokeMethod(String className){
        try {
            Class oa = Class.forName(className);
            //获得我们自定义的方法
            try {
                //获得welcom方法 (公开的static方法)
                Method method1 = oa.getMethod("welcome");
                //调用welcom方法,static方法不用传入要调用方法对象
                method1.invoke(null);
                
                //获得open方法 (私用的 对象方法)
                Method method2 = oa.getMethod("open");
                //调用,对象方法,要用这个类 创建一个对象传入,来确定是调用那个对象的对象方法
                method2.invoke(oa.newInstance());
                
                //获得save方法 有参数择传入参数的类型,对象方法带一个参数
                Method method3 = oa.getMethod("save", String.class);
                //调用
                method3.invoke(oa.newInstance(), "test-context");
                
                //获得save方法 有参数择传入参数的类型,对象方法多个参数
                Method method4 = oa.getMethod("save", int.class,String.class);
                //调用,第一个参数指明对象 后面参数安照方法声明一次写入
                method4.invoke(oa.newInstance(), 1,"test-context");
                
                
            } catch (Exception e) {
                e.printStackTrace();
            } 
            
            
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        
    }

Test 类中Main方法

    public static void main(String[] args) {
        getClassInfo("Word");
        invokeMethod("Word");
        System.out.println("-==================");
        invokeMethod("Excel");
    }
上一篇 下一篇

猜你喜欢

热点阅读