反射-得到对象的六种方式

  
        /*  1.Clss.forName
            已经知道一个类的全类名,
            应用场景:多用于配置文件,读取类全路径,加载类
        */
        final String classAllPath = "org.example.Car";
        Class<?> cls1 = Class.forName(classAllPath);
        System.out.println("cls1 "+cls1);

        /*  2.类名.class
            已经知道一个具体的类,
            应用场景:多用于参数传递,如通过反射得到对应的构造器对象
        */
        Class cls2 = Car.class;
        System.out.println("cls2 "+cls2);

        /*  3.getClass()
            已经知道一个类的实例,调用该实例的getClass()方法获取Class对象,
            应用场景:通过创建好的对象,获取Class对象
        */
        Car car = new Car();
        Class cls3 = car.getClass();
        System.out.println("cls3 "+cls3);

        /*  4.ClassLoader
            通过类加载器
            应用场景:
        */
        //(1)得到car类的加载器 
        ClassLoader classLoader = car.getClass().getClassLoader();
        //(2)通过类加载器,得到Class对象
        Class<?> cls4 = classLoader.loadClass("org.example.Car");
        System.out.println("cls4 "+cls4);


        //cls1 cls2 cls3 cls4其实是同一个对象
        System.out.println(cls1.hashCode());
        System.out.println(cls2.hashCode());
        System.out.println(cls3.hashCode());
        System.out.println(cls4.hashCode());

        //5.基本数据类型获取Class对象
        Class<?> integerClass = int.class;
        System.out.println(integerClass);

        //6.基本数据类型对应的包装类,通过Type获取Class对象
        Class<?> type = Integer.TYPE;
        System.out.println(type);

反射-获取类相关的信息

		Class<?> personClass = Class.forName("org.example.Person"); //父类
        Class<?> studentClass = Class.forName("org.example.Student"); //子类

        //获取全类名
        System.out.println("获取全类名"+personClass.getName());

        //获取简单类名
        System.out.println("获取简单类名"+personClass.getSimpleName());

        //获取所有public修饰的属性, 包括本类及父类
        Field[] fields = studentClass.getFields();
        System.out.println("获取所有public修饰的属性, 包括本类及父类");
        for (int i = 0; i < fields.length; i++) {
            System.out.println(fields[i].getName());
        }

        //获取本类的所有属性
        Field[] fields2 = personClass.getDeclaredFields();
        System.out.println("获取本类的所有属性");
        for (int i = 0; i < fields2.length; i++) {
            System.out.println(fields2[i].getName());
        }

        //获取本类和父类和超类所有public修饰的方法
        Method[] methods = studentClass.getMethods();
        System.out.println("获取本类和父类和超类所有public修饰的方法");
        for (int i = 0; i < methods.length; i++) {
            System.out.println(methods[i].getName());
        }

        //获取本类中所有的方法
        Method[] methods2 = studentClass.getDeclaredMethods();
        System.out.println("获取本类中所有的方法");
        for (int i = 0; i < methods2.length; i++) {
            System.out.println(methods2[i].getName());
        } 

        //获取本类所有public修饰的构造器,`
        Constructor[] constructors = studentClass.getConstructors();
        System.out.println("获取本类所有public修饰的构造器");
        for (int i = 0; i < constructors.length; i++) {
            System.out.println(constructors[i].getName());
        }

        //获取本类所有构造器
        Constructor[] constructors2 = studentClass.getDeclaredConstructors();
        System.out.println("获取本类所有构造器");
        for (int i = 0; i < constructors2.length; i++) {
            System.out.println(constructors2[i]);
        }

        //获取包名
        System.out.println("获取包名");
        System.out.println(studentClass.getPackage());

        //以Class的形式返回父类信息
        Class<?> superclass = studentClass.getSuperclass();
        System.out.println("以Class的形式返回父类信息");
        System.out.println(superclass);

        //以Class[]的形式返回接口信息
        Class[] interfaces = studentClass.getInterfaces();
        System.out.println("以Class[]的形式返回接口信息");
        for (int i = 0; i < interfaces.length; i++) {
            System.out.println(interfaces[i]);
        }

        //以Annotation[]的形式得到注解信息
        Annotation[] annotations = studentClass.getAnnotations();
        System.out.println("以Annotation[]的形式得到注解信息");
        for (int i = 0; i < annotations.length; i++) {
            System.out.println(annotations[i]);
        }