Java反射

Java反射

一、Java_Class类常用方法

案例演示

package com.njit.reflection.question;
import com.njit.Cat;
import java.lang.reflect.Field;

public class Class01 {
    public static void main(String[] args) throws Exception {

        Class aClass = Class.forName("com.njit.Cat");

        //1.输出属于哪个类
        System.out.println("1." + aClass);
        //2.得到包名
        System.out.println("2." + aClass.getPackage().getName());
        //3.得到全类名
        System.out.println("3." + aClass.getName());
        //4.通过aClass创建对象实例
        System.out.print("4.通过aClass创建对象实例\t");
        Cat cat = (Cat) aClass.newInstance();
        cat.cry();
        //5.通过反射获取属性
        Field name = aClass.getField("name");
        System.out.println("5." + name.get(cat));
        //6.通过反射给属性赋值
        name.set(cat,"看门狗");
        System.out.println("6." + name.get(cat));
        //7.获取所有的属性名称
        System.out.println("7.通过反射获取所有属性\t");
        Field[] fields = aClass.getFields();
        for (Field field : fields) {
            System.out.println(field.getName() + ":" + field.get(cat));
        }
    }
}

二、Java获取Class对象的六种方式

  1. 前提:已知一个类的全类名,且该类在类路径下,可通过Class类的静态方法forName()获取,可能抛出ClassNotFoundException

    Class cls1 = Class.forName("java.lang.Cat");
    

    应用场景:多用于配置文件,读取类全路径,加载类

  2. 前提:已知具体类,通过类的class获取,该方式最为安全可靠,程序性能最高

    Class cls2 = Cat.class;
    

    应用场景:多用于参数传递,比如通过反射得到对应构造器对象

  3. 前提:已知某个类的实例,调用该实例的getClass() 方法获取Class对象

    Class clazz = 对象.getClass();
    
  4. 其他方式

    ClassLoader cl = 对象.getClass().getClassLoader();
    Class clazz4 = cl.loadClass("类的全类名");
    

实例一:

package com.njit.reflection.question;

import com.njit.Cat;

public class GetClass_ {
    public static void main(String[] args) throws Exception {

        //1.Class.forName()
        String classAllPath = "com.njit.Cat";
        Class<?> cls1 = Class.forName(classAllPath);
        System.out.println(cls1);

        //2.类名.class
        Class cls2 = Cat.class;
        System.out.println(cls2);

        //3.对象.getClass(),应用场景,有对象实例
        Cat cat = new Cat();
        Class cls3 = cat.getClass();
        System.out.println(cls3);

        //4.通过类加载器(4)种来获取到类的class对象
        //(1)先得到类加载器 cat
        ClassLoader classLoader = cat.getClass().getClassLoader();
        //(2)通过类加载器得到class对象
        Class cls4 = classLoader.loadClass(classAllPath);
        System.out.println(cls4);

        System.out.println(cls1.hashCode());
        System.out.println(cls2.hashCode());
        System.out.println(cls3.hashCode());
        System.out.println(cls4.hashCode());

        //5.基本数据类型(byte,short,int,long,float,double,boolean,char)
        Class<Byte> byteClass = byte.class;
        Class<Short> shortClass = short.class;
        Class<Integer> integerClass = int.class;
        Class<Long> longClass = long.class;
        Class<Float> floatClass = float.class;
        Class<Double> doubleClass = double.class;
        Class<Boolean> booleanClass = boolean.class;
        Class<Character> characterClass = char.class;
        //6.基本数据类型的包装类,可以通过.TYPE得到Class对象
        Class<Byte> type = Byte.TYPE;
        Class<Short> type1 = Short.TYPE;
        Class<Integer> type2 = Integer.TYPE;
        Class<Long> type3 = Long.TYPE;
        Class<Float> type4 = Float.TYPE;
        Class<Double> type5 = Double.TYPE;
        Class<Boolean> type6 = Boolean.TYPE;
        Class<Character> type7 = Character.TYPE;
    }
}

实例二:

package com.njit.reflection.question;

import java.io.Serializable;

public class AllTypeClass {
    public static void main(String[] args) {

        Class<String> cls1 = String.class;//外部类
        Class<Serializable> cls2 = Serializable.class;//接口
        Class<Integer[]> cls3 = Integer[].class;//数组
        Class<float[][]> cls4 = float[][].class;//二维数组
        Class<Deprecated> cls5 = Deprecated.class;//注解
        Class<Thread.State> cls6 = Thread.State.class;//枚举
        Class<Long> cls7 = long.class;//基本数据类型
        Class<Void> cls8 = void.class;
        Class<Class> cls9 = Class.class;

        System.out.println(cls1);
        System.out.println(cls2);
        System.out.println(cls3);
        System.out.println(cls4);
        System.out.println(cls5);
        System.out.println(cls6);
        System.out.println(cls7);
        System.out.println(cls8);
        System.out.println(cls9);

    }
}
posted @ 2022-06-18 17:00  LiuTBaby  阅读(160)  评论(0编辑  收藏  举报