Saturday, September 09, 2008

动态类加载及反射演示类

package org.mytest;
 
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
 
/*
 * 动态类加载及反射演示类
 */
 
publicclass DynamicClassLoader {
    private String attribute = "我是DynamicClassLoader类的属性!";
    public String str2 = "hi";
 
    public String getAttribute() {
        returnattribute;
    }
 
    public DynamicClassLoader() {}
 
    publicvoid setAttribute(String attribute) {
        this.attribute = attribute;
    }
 
    publicint getAttribute2() {
        return 110;
    }
 
    protectedvoid protectedMethod() {}
 
    privatevoid privateMethod() {}
 
    publicstaticvoid main(String s[]) throws ClassNotFoundException,
            InstantiationException, IllegalAccessException,
            IllegalArgumentException, InvocationTargetException {
        // \\\\\\\\\\\\\\\\\\\\\\动态加载//////////////////
        DynamicClassLoader dcl0 = new DynamicClassLoader();// 这是我们通常创建一个类实例的做法--new
        System.out.println("new类实例,并打印实例dcl0的属性 = " + dcl0.getAttribute());
 
        Class class1 = Class.forName("org.mytest.DynamicClassLoader");
        DynamicClassLoader dcl1 = (DynamicClassLoader) class1.newInstance();
        System.out.println("Class.forName动态加载获得的类实例,并打印实例dcl1的属性 = " + dcl1.getAttribute());
 
        Class class2 = ClassLoader.getSystemClassLoader().loadClass("org.mytest.DynamicClassLoader");
        DynamicClassLoader dcl2 = (DynamicClassLoader) class2.newInstance();
        System.out.println("ClassLoader.loadClass动态加载获得的类实例,并打印实例dcl2的属性 = "    + dcl2.getAttribute());
 
        System.out.println("dcl1==dcl2" + (dcl1==dcl2));
        System.out.println("dcl1==dcl0" + (dcl1==dcl0));
        System.out.println("dcl2==dcl0" + (dcl1==dcl0));
       
        System.out.println("dcl0.getClass().getClassLoader()" + (dcl0.getClass().getClassLoader()));
        System.out.println("dcl1.getClass().getClassLoader()" + (dcl1.getClass().getClassLoader()));
        System.out.println("dcl2.getClass().getClassLoader()" + (dcl2.getClass().getClassLoader()));
        //讲解如果要求按照业务逻辑定义自己的对象比对策略(如hibernatepo可以仅仅比对
        //主键、主键等则两个po等),则重写(覆盖)equals方法和hashcode方法
       
        // ///////////////方法反射//////////////////
        DynamicClassLoader d = new DynamicClassLoader();
        Object o = (Object) d;
        System.out.println("o.getClass() = " + o.getClass());
 
        Method[] methods = o.getClass().getMethods();
        System.out.println("methods.length = " + methods.length);
        for (int i = 0; i < methods.length; i++) {
            System.out.print("method" + (i + 1) + ":\t");
            System.out.println(methods[i]);
        }
        System.out.println("---------------------");
 
        Method[] methods2 = o.getClass().getDeclaredMethods();
        System.out.println("methods2.length = " + methods2.length);
        for (int i = 0; i < methods2.length; i++) {
            System.out.print("method" + (i + 1) + ":\t");
            System.out.println(methods2[i]);
        }
   
        /////////////////穿插属性反射//////////////////
        System.out.println("--------穿插属性反射--------");
        Field[] fields = o.getClass().getDeclaredFields();
        System.out.println(fields.length);
        for(int i = 0; i<fields.length; i++){
            System.out.print("field" + (i + 1) + ":\t");
            System.out.println(fields[i]);
        }
        System.out.println("---------继续方法反射---------");
       
        /////////////////继续方法反射//////////////////
        for(int i = 0; i<methods.length; i++){
            System.out.print("method" + (i + 1) + ":\t");
            System.out.println(methods[i]);
            Method method = methods[i];
            String methodName = method.getName();
            String returnType = method.getReturnType().getName();
           
            if(methodName.startsWith("get")&&!methodName.startsWith("getClass")){
                if(returnType.equals("java.lang.String")){
                    String returnval = (String)method.invoke(d,null);
                    System.out.println("java.lang.String reval = "+returnval);
                }elseif(returnType.equals("String")){
                    String returnval = (String)method.invoke(d,null);
                    System.out.println("String reval = "+returnval);
                }elseif(returnType.equals("java.lang.Integer")){
                    Integer returnval = (Integer)method.invoke(d,null);
                    System.out.println("Integer reval = "+returnval);
                }elseif(returnType.equals("int")){
                    Integer returnval = (Integer)method.invoke(d,null);
                    System.out.println("int reval = "+returnval);
                }
            }
        }
    }
}
 
/*
 * 几点结论:
 * 对于private的方法getMethods()不反射、getDeclaredMethods()反射;
 * 对于protected的方法getMethods()不反射、getDeclaredMethods()反射;
 * getMethods()反射继承的和自身的public方法、getDeclaredMethods()反射自身声明的所有方法。
 */


Reactions

Abare | 2009-11-05 08:39 | IP:174.129.24.94

The hairs move dropping and splitting. On another hand propecia prescription controls DHT enzyme and gives hairs upbeat and protection. levitra online :DDD ambien online %( accutane 8-)


Leokadia | 2009-11-05 19:18 | IP:208.69.40.10

And if you deal with addicts and codependents and they are healthy enough of mind have them do it to. levitra wclrt buy accutane 629006 generic propecia 8-]


BeatenBlood | 2009-11-05 23:59 | IP:174.129.24.94

buy ambien :DD cialis 7336 xanax mwgii





Add a reaction

About You

Comment