我们都知道用户自定义的classloader 如果分别load两个class(class1,class2 ),他们之间的成员是不可以相互访问的,原因是他们是存在不同的命名空间中的,但是如果我非要在class1 中访问class2 的成员呢(非private)? 该怎么办?我把这句话给我老湿说,他很神秘的告诉我,只有唯一的绝招,反射! 然后就木有下文了,我瞬间就.... 去他一脸的反射! 我还是没懂,求大神!!!!!顺便可以讨论一下反射和类加载 有什么联系JVM反射ClassLoader类加载

解决方案 »

  1.   

    这没啥,就类似:
    A a = new A();
    B b = new B();如果都是public,完全可以直接访问:
    a.value = b.value;如果私有的,就要通过A或B的value对应的Field,再setAccessible(true);就能将其变成可访问的了。
      

  2.   

    啥也不说了,直接上酸菜package cn.com.reflect;public class Diaosi {

    private String language;

    protected int avNumber;

    public boolean teacherCang; public Diaosi() {
    // TODO Auto-generated constructor stub
    }

    public Diaosi(String language) {
    // TODO Auto-generated constructor stub
    this.language = language;
    }

    public String getLanguage() {
    return language;
    }

    public int getAvNumber() {
    return avNumber;
    }

    public String hehe() {
    return "你爱我吗?呵呵";
    }

    public String hehe(String nvshen) throws RuntimeException {
    return "屌丝的女神一般是" + nvshen;
    }}
    package cn.com.reflect;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;/**
     * 女神通过反射调用备胎
     * @author admin
     *
     */
    public class Nvshen { /**
     * @param args
     * @throws ClassNotFoundException 
     * @throws IllegalAccessException 
     * @throws InstantiationException 
     * @throws SecurityException 
     * @throws NoSuchMethodException 
     * @throws InvocationTargetException 
     * @throws IllegalArgumentException 
     * @throws NoSuchFieldException 
     */
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, NoSuchFieldException {
    // TODO Auto-generated method stub
    Class<?> clz = Class.forName("cn.com.reflect.Diaosi");
    //调用1,这个只要有默认构造函数即可
    Diaosi diaosi = (Diaosi) clz.newInstance();
    System.out.println(diaosi.hehe());

    //调用方式2
    //构造器信息
    Constructor<?> ctorlist[] = clz.getDeclaredConstructors(); 
            for (int i = 0; i < ctorlist.length; i++) { 
               Constructor<?> ct = ctorlist[i]; 
               System.out.println("构造器名称 = " + ct.getName()); 
               System.out.println("decl class = " + ct.getDeclaringClass()); 
               Class<?> pvec[] = ct.getParameterTypes(); 
               for (int j = 0; j < pvec.length; j++) 
                  System.out.println("参数 " + j + " " + pvec[j]); 
               Class<?> evec[] = ct.getExceptionTypes(); 
               for (int j = 0; j < evec.length; j++) 
                  System.out.println("抛出的异常 " + j + " " + evec[j]); 
            } 
    //获取类的方法
    Method[] methods = clz.getDeclaredMethods();
    for (int i = 0; i < methods.length; i++) {
    Method method = methods[i];
    System.out.println("method name:" + method.getName());
    //方法所在的类
    System.out.println("decl class = " + method.getDeclaringClass());
    //方法的參數信息
                Class<?> pvec[] = method.getParameterTypes(); 
                for (int j = 0; j < pvec.length; j++) 
                    System.out.println("参数" + j + "->" + pvec[j]); 
                //方法拋出的異常信息
                Class<?> evec[] = method.getExceptionTypes(); 
                for (int j = 0; j < evec.length; j++) 
                    System.out.println("异常 " + j + "->" + evec[j]); 
                System.out.println("return type = " + method.getReturnType());
    }
    System.out.println();
    //获取类的字段信息
    Field fieldlist[] = clz.getDeclaredFields(); 
            for (int i = 0; i < fieldlist.length; i++) { 
               Field fld = fieldlist[i]; 
               System.out.println("字段名 = " + fld.getName()); 
               System.out.println("字段类型 = " + fld.getType()); 
               int mod = fld.getModifiers();
               //public protected private
               System.out.println("修饰符 = " + Modifier.toString(mod)); 
            }
            System.out.println();
            //执行指定的方法
            Method method = clz.getMethod("hehe");//无参的呵呵
            Object object = method.invoke(clz.newInstance());
            System.out.println(String.valueOf(object));
            method = clz.getMethod("hehe", String.class);//带参的呵呵
            object = method.invoke(clz.newInstance(), "凤姐");
            System.out.println(object);
            
            Field field = clz.getDeclaredField("language");
            //私有属性访问,public的不用加这个
            field.setAccessible(true);
            System.out.println(field.get(diaosi));
            field.set(diaosi, "雅蠛蝶");
            System.out.println(field.get(diaosi));
            field.setAccessible(false);
    }}