Java 注解 反射

注解:Java.lang.annotation;

1.定义: public @interface 注解名{String value(); int age();int id() default ""};

2.编译器中注解:@Override  @Deprecated  @SuppressWarnings   @Target( ElementType={ })  @Rentation(RentationPolicy={}) @Documented @Inherited

3. 自定义注解 @interface test{         }

反射:java.lang.reflection   java.lang.Class     (Pojo  属性  set/get   无参构造,  toString())

1.反射机制:获取类的任何内部信息,直接操作属性和方法。 

2.Class类和获取Class类实例 :  类.Class    对象.getClass()  Class.forName("  ") Integer.Type   类.class.getSuperClass();

                1.Class 本身也是一个类

                2.Class对象只能有系统建立对象

                3.一个加载的类在JVM中只会有一个Class实例

                4.一个Class对象对应的事一个加载到JVM里的一个.class文件

                5.每个类的实例都会记得自己是由哪个Class实例所生成的。

                6.通过Class可以完整的得到一个类中的所有被加载的结构

                7.Class类的Reflection的根源,针对任何你想动态加载、运行的类,唯有先获得相应的Class对象。

3.类的加载与ClassLoader

       1.加载.class 文件  2.验证:class文件是否符合jvm规范;准备:static,类变量 分配内存;解析:

  3.初始化:《clint》

   ClassLoader.getSystemclassloader();

        System.getProperty("java.class.path")

    根加载器null ----->   扩展类加载器  -------->   系统类加载器

4.创建运行时类的对象

      Class 

   getName() 

   getSimpleName() 

    getFields()    getDeclaredFields()

    getMehtods()  getDeclaredMthods()   getMethod("方法名","参数类型.class")

  getConstruct()

    newInstance()

    属性和方法都有setAcessible(true)

5.获取运行时类的完整结构

 f= Class . getAnotation(注解.class)     f.value();

  Field .getAnnotation(注解.class)      

6.调用运行时类的指定结构

猜你喜欢

转载自www.cnblogs.com/chencn/p/12234363.html
今日推荐