Java注解和反射

目录

注解

介绍

内置注解

元注解

自定义注解

反射

概述

Class类


​​​​​​​

注解

介绍

Annotation作用:可以被程序读取的注释

格式:@注释名(可以加参数)

使用的地方:可以加在package、class、method、field等上面

通过反射机制编程实现对这些元数据的访问

内置注解

@Override 重写父类的方法

@Deprecated 不推荐使用,但是可以使用或者有更好的方法

@SuppressWarnings("all") 镇压代码中的警告信息

元注解

作用:负责注解其他注解

@Target(value = {ElementType.METHOD,等等}) 描述注解使用范围

@Retention(RetentionPolicy.RUNTIME) 描述注解的生命周期,在什么范围内有效(SOURCE < CLASS < RUNTIME)

@Documented 说明该注解将被生成包含在Javadoc中

@Inherited 说明子类可以继承父类中的该注解

自定义注解

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

//自定义注解
public class CustomAnnotation {
    @MyAnnotation(age = 18,schools = "南京大学",name = "张三")
    @MyAnnotation1("吃饭") //只有当注解只有一个参数名为value的参数时,使用的时候才可以省略参数名直接赋值
    public void test(){

    }
}

@Target({ElementType.TYPE,ElementType.METHOD})//使用范围:可以在类上,也可以在方法上
@Retention(RetentionPolicy.RUNTIME) //生命周期到运行时
@interface MyAnnotation{
    //注解的参数:参数类型 + 参数名();
    String name() default "";//默认值为空,使用时可以显式赋值,也可以不写
    int age(); //没有定义默认值,使用该注解时就必须赋值
    String[] schools() default {"清华大学","东南大学"};
}

@Target({ElementType.TYPE,ElementType.METHOD})//使用范围:可以在类上,也可以在方法上
@Retention(RetentionPolicy.RUNTIME) //生命周期到运行时
@interface MyAnnotation1{
    String value();
}

反射

概述

Reflection(反射)是java被视为准动态语言的关键,反射机制允许程序在执行期借助Reflection API取得任何类的内部信息,并能直接操作任意对象的内部属性及方法。

正常方式:引入需要的“包类”名称 ----> 通过new实例化 ---> 取得实例化对象

反射方式:实例化对象 ---> getClass()方法 ---> 得到完整的“包类”名称

反射相关主要API:

  • java.lang.Class:代表一个类

  • java.lang.reflect.Method:代表类的方法

  • java.lang.reflect.Field:代表类的成员变量

  • java.lang.reflect.Constructor:代表累的构造器

//什么是反射
public class Test1 {
    public static void main(String[] args) throws ClassNotFoundException {
        //通过反射获取class对象
        Class c1 = Class.forName("com.water.reflection.User");
        System.out.println(c1);

        Class c2 = Class.forName("com.water.reflection.User");
        Class c3 = Class.forName("com.water.reflection.User");
        Class c4 = Class.forName("com.water.reflection.User");

        //一个类在内存中只有一个Class对象
        //一个类被加载后,类的整个结构都会被封装在Class对象中
        System.out.println(c2.hashCode()); //1956725890
        System.out.println(c3.hashCode()); //1956725890
        System.out.println(c4.hashCode()); //1956725890
    }
}

//实体类 pojo , entity
class User{
    private int id;
    private String name;
    private int age;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

Class类

//测试Class类的创建方式有哪些
public class Test2 {
    public static void main(String[] args) throws ClassNotFoundException {
        Person person = new Student();
        System.out.println("这个人是:" + person.name);

        //方式一:通过对象获得
        Class c1 = person.getClass();
        System.out.println(c1);
        System.out.println(c1.hashCode());

        //方式二:forName获得
        Class c2 = Class.forName("com.water.reflection.Student");
        System.out.println(c2.hashCode());

        //方式三:通过类名.class获得
        Class c3 = Student.class;
        System.out.println(c3.hashCode());

        //方式四:基本内置类型的包装类都有一个TYPE属性
        Class c4 = Integer.TYPE;
        System.out.println(c4);

        //方式五:获得父类类型
        Class c5 = c1.getSuperclass();
        System.out.println(c5);

    }
}

class Person{
    public String name;

    public Person(String name) {
        this.name = name;
    }

    public Person() {
    }
}

class Student extends Person{
    public Student() {
        this.name = "学生";
    }
}

class Teacher extends Person{
    public Teacher() {
        this.name = "老师";
    }
}

哪些类型有Class对象:

  • class:外部类,成员(成员内部类、静态内部类),局部内部类,匿名内部类

  • interface:接口

  • []:数组

  • enum:枚举

  • annotation:注解@interface

  • primitive type:基本数据类型

  • void

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值