Java的高级篇--反射

1.泛型和注解的回顾

1. java高级--泛型.
   [1]泛型类
   [2]通配符?
   [3]设置泛型上限下限。
      参数: 类名<? extends 类名>
      定义类: public class 类名<T extends 类名>{}
   [4]泛型接口
   [5]泛型方法。
2.java高级---注解Annotation
   [1]预定义注解---JDK系统自带
   [2]元注解-----定义在注解上的注解。@Target @Document @Retention @In
   [3]自定义注解
      语法:
      public @interface 注解名{
          //注解属性;
          数据类型 属性名() default 默认值;
          //基本类型,字符串类型,枚举类型,注解类型,数组类型[]
      }
      使用注解:
      @注解名(属性值)

2.反射的内容

1. 什么是反射?
2. 如何获取反射类Class
3. 根据反射类如何创建对应的类对象。
4. 如何获取Field类对象
5. 如何获取Method类对象。
6. 如何获取对应的Annotation注解对象

3.什么是反射

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

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

4.如何获取反射类对象--class

有三种:

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

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

(2)通过类名.class获取

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

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

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

/**
 * @unthor : YSH
 * @date : 14:42 2022/7/14
 */
public class Test01 {

    public static void main(String[] args) throws ClassNotFoundException {
        //1.通过Class.forName来获取反射对象
        Class aClass = Class.forName("demo01.People");

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

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


        //思考:上面三个反射对象的引用地址是否一致! 是一致的。 一个类只会被加在到内存中一次。
        System.out.println(aClass==aClass1);
        System.out.println(aClass1==aClass2);
    }
}

5. 通过反射类获取对应的类对象。

aclass.newInstance();

package demo02;

/**
 * @unthor : YSH
 * @date : 14:56 2022/7/14
 */
/*通过反射类获取对应的类对象*/
public class Test02 {
    public static void main(String[] args) throws Exception{
        /*1获取反射类对象*/
        Class<People> peopleClass = People.class;

        //2.由反射类创建对象---调用无参构造函数
        People people = peopleClass.newInstance();
        People people1 = peopleClass.newInstance();

        //他们的地址是否相同
        System.out.println(people==people1);
    }
}

6. 通过反射获取对应的Field属性对象。

package demo03;

import java.lang.reflect.Field;

/**
 * @unthor : YSH
 * @date : 15:10 2022/7/14
 */
/*通过反射获取对应的Field属性对象。*/
public class Test03 {
    public static void main(String[] args) throws NoSuchFieldException {
        Class<People> aClass = People.class;

        /*1.获取本类指定的属性对象*/
        Field name = aClass.getDeclaredField("name");
        System.out.println(name);

        /*获取本类以及父类中指定的属性--必须为public修饰的*/
        Field sex = aClass.getField("sex");
        System.out.println(sex);


        /*获取本类中所有的属性对象*/
        Field[] declaredFields = aClass.getDeclaredFields();
        for (Field field: declaredFields
             ) {
            System.out.println(field);
        }

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

6.2 Field属性对象中常见的方法.

getName():获取属性名
setAccessible(true):设置属性可访问。
set(o,v):为对象o的属性赋值v
get(o):获取对象o的属性值

扫描二维码关注公众号,回复: 14411981 查看本文章
public static void main(String[] args) throws Exception {
        Class<People> peopleClass = People.class;

        Field nameField = peopleClass.getDeclaredField("name");
        //1.获取属性的名称
        String name = nameField.getName();
        System.out.println("属性名称:"+name);

        //2.为属性赋值.
        //Object obj,对象
        // Object value
        People people = peopleClass.newInstance();
        System.out.println(people);

        nameField.setAccessible(true);//为nameField设置可访问权限,打破了封装性
        nameField.set(people,"张三");
        System.out.println(people);

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

        //nameField.getAnnotation();//获取nameField属性上的注解对象
    }

7. 通过反射获取对应的Method方法对象

package demo3;

import demo.People;

import java.lang.reflect.Method;

/**
 * @program: qy151-java高级-反射
 * @description:
 * @author: YSH
 * @create: 2022-07-14 15:50
 **/
public class Test03 {
    public static void main(String[] args) throws Exception {
        Class<People> aClass = People.class;

        //获取本类中所有的方法。
        Method[] declaredMethods = aClass.getDeclaredMethods();
        for (Method method:declaredMethods) {
            System.out.println(method);
        }


        System.out.println("======================================================");
        //获取本类和父类中所有public修饰的方法
        Method[] methods = aClass.getMethods();
        for (Method method : methods) {
            System.out.println(method);
        }

        System.out.println("**************************************************************");
        //本类中指定的 方法对象
        Method fun = aClass.getDeclaredMethod("fun");
        System.out.println(fun);
        Method setName = aClass.getDeclaredMethod("setName",String.class);
        System.out.println(setName);

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

    }
}

7.2 Method类中常见的方法。

  //method.invoke(对象,方法参数值);
 invoke(people,"15");//回调。动态代理 

8. 获取相应注解对象

package demo04;

import java.lang.reflect.Field;j

/**
 * @program: qy151-java高级-反射
 * @description:
 * @author: YSH
 * @create: 2022-07-14 16:15
 **/
public class Test {
    public static void main(String[] args) throws Exception{
        Class<Student> aClass = Student.class;

        //获取类对象的指定注解
        MyAnnotation annotation = aClass.getAnnotation(MyAnnotation.class);
        System.out.println(annotation.value());


        Field idField = aClass.getDeclaredField("id");
        MyAnnotation annotation1 = idField.getAnnotation(MyAnnotation.class);
        System.out.println(annotation1.value());
        System.out.println(annotation1.sex());

    }
}

猜你喜欢

转载自blog.csdn.net/Ysuhang/article/details/125803008