Java高级-反射(获取反射类Class,根据反射类创建对应的类对象,获取Field类对象,获取Method类对象,获取对应的Annotation注解对象)

一、什么是反射?

        反射是框架设计的灵魂,框架:它是一个半成品,可以拿来使用,添加上自己的业务代码。提高开发效率。

反射就是把类中成员抽取成其他类的过程。这就是反射。

二、如何获取反射类对象(3种方式)

1.通过Class.forName获取反射对象.

Class.forName("全路径")
--spring它就是使用的该模式<bean class="全路径">

//People是我定义的一个类
 //1.通过Class.froName获取
        Class aClass = Class.forName("com.gsh.getReflection.People");

2.通过类名.class获取

类名.class;
---代理类--->SqlSession.getMapper(StudentDao.class)

 //2.通过类名调用.class来获取反射类对象
        Class peopleClass = People.class;

3.通过对象.getClass()方法获取

对象.getClass();
---当知道对象时可以通过这种方式获取反射对象

//3.通过对象获取反射类
        People people=new People();
        Class aClass1 = people.getClass();

注: 上面三个反射对象的引用地址是否一致?

是一致的。 一个类只会被加在到内存中一次。

 三、通过反射类获取相应的类对象

反射类.newInstance();

注:由反射类创建类对象需要调用无参构造

 public static void main(String[] args) throws IllegalAccessException, InstantiationException {
        //获取反射类对象
        Class<People> peopleClass = People.class;
        //2.由反射类创建类对象,调用无参构造
        People people = peopleClass.newInstance();
        People people1=peopleClass.newInstance();
        //3.判断它们地址是否相同
        System.out.println(people==people1);
    }

四、 通过反射获取对应的Field属性对象

1.获取本类中指定的属性对象

反射类.getDeclaredField("属性名")

2.获取本类以及父类中指定的public修饰的属性

反射类.getField("属性名")

3.获取本类中所有的属性对象

反射类.getDeclaredFields("属性名")

4.获取本类以及父类中所有public修饰的属性

反射类.getFields("属性名")

    public static void main(String[] args)throws Exception {
        //获取反射类对象
        Class<People> peopleClass = People.class;
        //1.获取本类以及父类中指定的公有属性public
        Field name = peopleClass.getField("name");
        System.out.println(name);

        //2.获取本类中指定的的属性
        Field declaredField = peopleClass.getDeclaredField("age");
        System.out.println(declaredField);

        //3.获取本类中的所有属性
        Field[] declaredFields = peopleClass.getDeclaredFields();
        for (Field field : declaredFields) {
            System.out.println(field);
        }
        System.out.println("*******************");
        //获取本类及父类中所有的public修饰的属性对象
        Field[] fields = peopleClass.getFields();
        for (Field field : fields) {
            System.out.println(field);
        }
    }

 五、Field属性对象中常见的方法

1.获取属性名

属性对象.getName():

2.设置属性可访问(会打破封装)

属性对象.setAccessible(true);

3.为对象的属性赋值

属性对象.set(对象,值)

属性对象为你获取到的属性对象,对象为你要赋值属性所属的对象。

4.获取对象的属性值

属性对象.get(对象)

这里属性对象为你获取到的属性对象,对象为你要获取属性值所属的对象。

 //获取反射类对象
        Class<People> peopleClass = People.class;
        //获取指定属性
        Field fieldname = peopleClass.getDeclaredField("name");
        //1.获取属性的名称
        String name = fieldname.getName();
        System.out.println(name);


        //2.为属性赋值
        People people = peopleClass.newInstance();
        //为fieldname设置可访问权限,打破封装
        fieldname.setAccessible(true);
        //第一个参数是:对象
        //第二个参数是:要赋的值
        fieldname.set(people,"李四");
        System.out.println(people);

        //3.获取属性值
        Object o = fieldname.get(people);
        System.out.println(o);

六、通过反射获取对应的Method方法对象

1.获取本类中所有的方法:

反射类.getDeclaredMethods();

得到的是一个存放所有方法的数组

2.获取本类和父类中所有public修饰的方法

反射类..getMethods();

得到的是一个存放所有方法的数组

3.获取本类中指定的方法对象

反射类.getDeclaredMethod("方法名",方法参数);

4.获取本类及父类中指定名称的方法对象

反射类.getMethod("方法名", 方法参数);

public static void main(String[] args)throws Exception {
        //获取反射类
        Class<People> peopleClass = People.class;
        //1.获取本类及父类中所有public修饰的方法
        Method[] methods = peopleClass.getMethods();
        for(Method method:methods)
        System.out.println(method);
        System.out.println("_---------------------------------");
        //2.获取本类中所有的方法
        Method[] declaredMethods = peopleClass.getDeclaredMethods();
        for(Method method:declaredMethods)
        {
            System.out.println(method);
        };
        System.out.println("******************");
        //3.获取本类中指定的方法对象
        Method setName = peopleClass.getDeclaredMethod("setName", String.class);
        System.out.println(setName);
        System.out.println("!!!!!!!!!!!!!!!!!!!!!!");

        //4.获取本类及父类中指定名称的方法对象
        Method method = peopleClass.getMethod("equals", Object.class);
        System.out.println(method);
    }

 七、Method类中常见的方法

回调,动态代理中也使用了回调

method.invoke(对象,方法参数值);

八、获取相应的注解对象

1.获取

反射类.getAnnotation(注解名.class)

通过反射类获取到的属性.getAnnotation(注解名.class)

2.取出注解中的value值

获取的注解对象.value()

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值