注解学习:annotation

• Annotation是从JDK5.0开始引入的新技术。
• Annotation的作用:
– 不是程序本身,可以对程序作出解释。(这一点,跟注释没什么区别)
– 可以被其他程序(比如:编译器等)读取。(注解信息处理流程,是注解和注释的重大区别 。如果没有注解信息处理流程,则注解毫无意义)
• Annotation的格式:
– 注解是以“@注释名”在代码中存在的,还可以添加一些参数值,例如: @SuppressWarnings(value=“unchecked”)。
• Annotation在哪里使用?
– 可以附加在package, class, method, field等上面,相当于给它们添加了额外的辅助信 息,我们可以通过反射机制编程实现对这些元数据的访问‘’

@Override
– 定义在java.lang.Override中,此注释只适用于修辞方法,表示一个方 法声明打算重写超类中的另一个方法声明。
@Deprecated
– 定义在java.lang.Deprecated中,此注释可用于修辞方法、属性、类 ,表示不鼓励程序员使用这样的元素,通常是因为它很危险或存在更 好的选择
@SuppressWarnings
– 定义在java.lang.SuppressWarnings中,用来抑制编译时的警告信息 。
– 与前两个注释有所不同,你需要添加一个参数才能正确使用,这些参 数值都是已经定义好了的,我们选择性的使用就好了,参数如下
在这里插入图片描述
示例:
– @SuppressWarnings(“unchecked”)
– @SuppressWarnings(value={“unchecked”, “deprecation”})

自定义注解:
在这里插入图片描述
注解需要对元注解对其进行解释:
在这里插入图片描述
target的作用,主要表明注解用于什么地方,例如:类,方法。
在这里插入图片描述
retention则用来表示注解在什么时候有效。
在这里插入图片描述

public class Demo02 {
    @LsdAbbotation01(age=19,studentName = "lsd",id=1001,
            schools = {"北京","杭州"})
    public void test(){
    }
    @LsdAootation02("name")
    public void test2(){

    }
}

/**
 * @Target表达修饰范围
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(value= {ElementType.METHOD,ElementType.TYPE})
public @interface LsdAbbotation01 {
    //加参数
    String studentName() default "";
    int age() default 0;
    int id() default -1;//不存在
    String[] schools() default {"杭州","上海"};
}

注解本身没有什么意义,只有通过解析才会有意义。

示例:
定义类注解:

@Retention(RetentionPolicy.RUNTIME)
@Target(value={ElementType.TYPE})
public @interface LsdTable {
    String value();
}

定义字段注解:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface LsdField {
    String columnName();
    String type();
    int length();
}

类中使用注解,赋值:

@LsdTable("lsd_student")
public class LsdStudent {
    @LsdField(columnName = "name",type = "varchar",length = 10)
    private String name;
    @LsdField(columnName = "id",type = "int",length = 10)
    private int id;
    @LsdField(columnName = "age",type = "int",length = 3)
    private int age;

    public String getName() {
        return name;
    }

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

    public int getId() {
        return id;
    }

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

    public int getAge() {
        return age;
    }

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

通过反射加载类和属性,创建表:

/**
 * 使用反射读取注解信息,模拟处理注解的流程
 * @author lsd
 */
public class Demo03 {
    public static void main(String[] args) {
       try {
           Class clazz=Class.forName("annotation.LsdStudent");
           //获取类所有注解
           Annotation[] annotations=clazz.getAnnotations();
           for(Annotation annotation:annotations){
               System.out.println(annotation);
           }
           //获得类指定注解的值
           LsdTable lsdTable= (LsdTable) clazz.getAnnotation(LsdTable.class);
           System.out.println(lsdTable.value());

           //获得类的属性的注解

           Field field=clazz.getDeclaredField("name");
           LsdField lsdField= field.getAnnotation(LsdField.class);
           System.out.println(lsdField.columnName()+"--"+lsdField.type()+"--"+lsdField.length());

           //根据获得表怒部分,字段信息,生成相关表
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
           e.printStackTrace();
       }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值