java 注解的基本使用

1.注解含义:注解其实就是代码里特殊的标志,这些标志可以在编译,类加载,运行时被读取,并执行相应的处理,注解可以像修饰符一样使用,可用于修饰包,类,构造器,
2.自定义注解

package com.yl.pdfdemo.day08.p1;

import java.lang.annotation.*;

import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.ElementType.LOCAL_VARIABLE;

/**
 * @Author wfj
 * @Date 2021/6/11
 * @Description 自定义注解
 * @Version 1.0
 */

@Documented
@Inherited
@Repeatable(MyAnnotations.class)
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE, TYPE_PARAMETER,TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
    /**
     * 如何自定义注解:
     * 1.注解声明为@interface
     * 2.内部定义成员通常用value表示
     * 3.可以指定成员的默认值,使用default定义
     * 4.如果自定义注解没有成员,那么其就是一个标识作用
     * 5.如果注解有成员,那么在使用注解时,需要指明成员的值
     *  自定义注解必须结合反射,才有意义,其一般用到Retention和Target这两个元注解来修饰
     */

    String value() default "vue";
}

package com.yl.pdfdemo.day08.p1;

import java.lang.annotation.*;

import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.ElementType.LOCAL_VARIABLE;

/**
 * @Author wfj
 * @Date 2021/6/11
 * @Description 自定义注解
 * @Version 1.0
 */
@Documented
@Inherited
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotations {
    MyAnnotation[] value();
}

3.通过反射获取注解相关信息

package com.yl.pdfdemo.day08.p1;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author wfj
 * @Date 2021/6/10
 * @Description 注解
 * @Version 1.0
 */

public class AnnotationTest {
    /**
     * 1.jdk5.0开始,java增加了对元数据的支持,也就是注解
     * 注解其实就是代码里特殊的标志,这些标志可以在编译,类加载,运行时被读取,并执行相应的处理
     * 注解可以像修饰符一样使用,可用于修饰包,类,构造器,
     *
     * 2.Annotation的使用示例
     * 1)生成文档相关的注解
     * 2)@Override限定重写方法,改注解只能用于方法 @Deprecated表明过时方法或者其他过时属性等 @SuppressWarnings 抑制编译器警告
     * 3)跟踪代码依赖性,实现替代配置文件功能
     *
     * 3.jdk提供的4种元注解
     * 元注解:对现有的注解进行解释说明的注解
     * 1)Retention:指定所修饰的Annotation的生命周期,SOURCE,CLASS(默认),RUNTIME
     *      只有声明为RUNTIME声明周期的注解,才能通过反射获取
     *
     * 2)Target:用于指定被修饰的Annotation能用于修饰那些程序元素
     *    @Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE}) 表明类,接口,属性,方法,构造器等等。。。
     * 3)Documented:表示所修饰的注解在被javadoc解析时,保存下来
     * 4)Inherited:被它修饰的Annotation将具有继承性
     *
     * 4.jdk8中注解的新特性:可重复注解,类型注解
     * 1)可重复注解
     *      1.1)在MyAnnotation上声明@Repetable,成员值为MyAnnotations.class
     *      1.2)MyAnnotation的元注解要和MyAnnotations元注解的相同
     * 2)类型注解
     *  TYPE_PARAMETER(泛型),TYPE_USE
     *
     */
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.walk();

        @SuppressWarnings("unused")
        int num = 20;

        //通过反射获取属性
        Class clazz = Dog.class;
        Annotation[] annotations = clazz.getAnnotations();
        for (int i = 0; i < annotations.length; i++) {
            System.out.println(annotations[i]);//@com.yl.pdfdemo.day08.p1.MyAnnotations(value=[@com.yl.pdfdemo.day08.p1.MyAnnotation(value=java), @com.yl.pdfdemo.day08.p1.MyAnnotation(value=typescript)])
        }
    }

}

//jdk8之前的写法
//@MyAnnotations({@MyAnnotation(value = "js")},@MyAnnotation(value = "html"))
@MyAnnotation(value = "java")
@MyAnnotation(value = "typescript")
class Animal{
    private String name;
    private int age;

    public Animal() {
    }
    public void walk() {
        System.out.println("animal walk");
    }
}

class Dog extends Animal{
    public Dog() {
    }

    @Override
    public void walk() {
        System.out.println("Dog walk..");
    }
}

//类型注解
class Geric<@MyAnnotation T> {

    public void show() throws @MyAnnotation RuntimeException{
        List<@MyAnnotation String> list = new ArrayList<>();
        int number = (@MyAnnotation int)100L;
    }


}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值