反射

Class类的使用

类是对象,类是java.lang.Class类的实例对象

public class fanShe {
    public void main(String[] args) {
        /*在java中万事万物皆为对象,每一个类都是Class类的实例对象,但是不能通过new的方式获取Class的对象,因为Class类是私有的,
        只能由java虚拟机来创建,而我们还可以通过其它方法来创建
        * */
        //方式一:每一个类都有默认的一个class静态成员
        Class c1 = Foo.class;
        //方式二:可以通过该类的对象,调取getClass()方法来获取
        Foo foo = new Foo();
        Class c2 = foo.getClass();
        //总结:c1 c2表示该类的类类型,万事万物皆为对象,每一个类都是Class类的实例对象,这个对象我们称它是该类的类类型
        //方式三:通过Class的forName方法
        try {
            Class c3 = null;
            c3 = Class.forName("fanshe");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        //我们可以直接使用该类的类类型即c1 c2 c3直接创建该类的实例对象
        try {
            Foo foo1 = (Foo)c1.newInstance();
            foo1.print();
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
    class Foo{
        void print() {
            
        }
    }
}

方法的反射

package fs;
import java.lang.reflect.Method;
public class ClassUtils {
    public static void main(String[] args) {
        String s1 ="Hello";
        Integer i1 = 11;
        printClassMessage(i1);
    }
    //打印类的对象,包括类的成员函数,成员变量
    public static void printClassMessage(Object obj){
        //通过该类的对象获取该类的类类型
        Class c = obj.getClass();
        //保存一下类的方法信息
        //getMethods可以获取该类所有的public方法,以及继承来的public方法
        //getDeclaredMethods()可以获取该类的所有方法
        Method[] mes = c.getMethods();
        for (Method me : mes) {
            //得到返回值类型的类类型
            Class retunType = me.getReturnType();
            System.out.print(retunType.getName()+" ");
            System.out.print(me.getName() + "(");
            //获取参数列表的类类型
            Class[] ParameterType = me.getParameterTypes();
            for (Class aClass : ParameterType) {
                System.out.print(aClass.getName());
            }
            System.out.println(")");
        }
    }
}

成员变量的反射

Field[] fs = c.getDeclaredFields();
for (Field f : fs) {
    //获取成员类型的类类型
    Class fieldType = f.getType();
    //获取成员变量类型名字
    String typeName = fieldType.getName();
    //获取成员变量名字
    String fieldName = f.getName();
    System.out.println(typeName+"\t"+fieldName);

构造函数的反射

package fs;
import java.lang.reflect.Constructor;
public class Demo01 {
    public static void main(String[] args) {
        new Demo01().printConMes("hell");
    }
        public void printConMes(Object o){
            //通过对象获取类类型
            Class c = o.getClass();
            //java.lang.constructor封装了构造函数的类类型
            Constructor[] cs = c.getDeclaredConstructors();
            for (Constructor constructor : cs) {
                System.out.print(constructor.getName()+ " ");
                Class[] pt = constructor.getParameterTypes();
                for (Class aClass : pt) {
                    System.out.print(aClass.getName()+",");
                }
            }
        }
    }

方法的反射

package fs;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Demo02 {
    public static void main(String[] args) {
        //获取类的类类型
        Demo02 demo02 = new Demo02();
        Class c = demo02.getClass();
        //获取方法的类类型

        try {
            Method me = c.getMethod("print",int.class,int.class);
            try {
                me.invoke(demo02,1,2);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            } catch (InvocationTargetException e) {
                throw new RuntimeException(e);
            }
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        }


    }

    public void print(int a,int b){
        System.out.println(a+b);
    }
    public void print(String a,String b){
        System.out.println(a.toUpperCase()+b.toLowerCase());
    }
}

java类加载机制

package fs;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

public class Demo03 {
    public static void main(String[] args) {
        //集合中泛型的本质
        ArrayList<String> strings = new ArrayList<>();
        ArrayList<Integer> ints = new ArrayList<>();
        strings.add("你好");
        Class c1 = strings.getClass();
        Class c2 = ints.getClass();
        //输出了结果是true说明集合在编译阶段进行了去泛型
        System.out.println(c1 == c2);
        //拿到类类型
        Class c = strings.getClass();
        try {
            Method me = c.getMethod("add",Object.class);
            try {
                me.invoke(strings,5);
                System.out.println(strings.toString());
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            } catch (InvocationTargetException e) {
                throw new RuntimeException(e);
            }
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        }
    }
}
posted on 2022-07-31 18:48  clinch  阅读(26)  评论(0编辑  收藏  举报