Java注解入门

一.Java注解的定义

定义:注解(Annotation),也叫元数据。一种代码级别的说明。它是JDK1.5及以后版本引入的一个特性,与类、接口、枚举是在同一个层次。它可以声明在包、类、字段、方法、局部变量、方法参数等的前面,用来对这些元素进行说明,注释。

二.最常用的注解

@Deprecated(方法过时),@SuppressWarnings(“deprecation”)(忽视过时的方法)、@Override(重载)

1.@Deprecated(方法过时)

当某方法作为jar包不想以后的人继续使用的时候,我们将它过时(@deprecated)
例如:

    @Deprecated 
    public static void sayHello()
    {

        System.out.println("hi,传智博客");

    }
//过时的方法被调用会有下面横杆的提示
        AnnotationTest.sayHello();

这样用命令行编译的时候,就有警告⚠️;

如果不想有警告的话我们可以在方法前添加:

    @SuppressWarnings("deprecation")
    public static void main(String[] args) {

        Test.sayHello();

    }

2.@override重载

例如:

    @Override
    public int hashCode() {
        // TODO Auto-generated method stub
        return super.hashCode();
    }

这样避免出错

    @Override//假如没有重载的方法添加了override标签就会报错
    public int hashCode(Object obj) {
        // TODO Auto-generated method stub
        return super.hashCode();
    }

接下来开始注解的入门了

注解就相当于一个要调用的一个类,要在源程序中应用某个注解,得先准备某个注解,得先准备好了这个注解类.

这里写图片描述

自定义注解及其应用
  • 定义一个最简单的注解
public @interface MyAnnotation{}
  • 把他加载在某个类上:@MyAnnotation publicl class AnnotationTest{}

可是,使用方式回去该注解的时候不存在

  • 因为根据发射测试的问题,引出@Retention元注解的讲解
    ,其三种取值:
public enum RetentionPolicy {
    /**
     * Annotations are to be discarded by the compiler.
     *注解在编译就被抛弃了,只存在于源文件中
     */
    SOURCE,

    /**
     * Annotations are to be recorded in the class file by the compiler
     * but need not be retained by the VM at run time.  This is the default
     * behavior.
     * 注解记录在class文件中编译中,不过不能再虚拟机和运行中保留,这个是默认的行为
     */
    CLASS,

    /**
     * Annotations are to be recorded in the class file by the compiler and
     * retained by the VM at run time, so they may be read reflectively.
     * Anntations are to be recorded in the class
     * file by the compiler and retained by the VM at run time,so they may read reflectively
     * @see java.lang.reflect.AnnotatedElement
     */
    RUNTIME
}

限定注解的范围 @Taget(ElemetType.xxx)

public enum ElementType {
    /** Class, interface (including annotation type), or enum declaration */
    TYPE,

    /** Field declaration (includes enum constants) */
    FIELD,

    /** Method declaration */
    METHOD,

    /** Formal parameter declaration */
    PARAMETER,

    /** Constructor declaration */
    CONSTRUCTOR,

    /** Local variable declaration */
    LOCAL_VARIABLE,

    /** Annotation type declaration */
    ANNOTATION_TYPE,

    /** Package declaration */
    PACKAGE,

    /**
     * Type parameter declaration
     *
     * @since 1.8
     */
    TYPE_PARAMETER,

    /**
     * Use of a type
     *
     * @since 1.8
     */
    TYPE_USE
}

以上就


学习了注解的定义,使用范围,和通过反射获得注解,之后就是注解的应用了


三.注解的应用

属性的定义:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)//限制注解的使用范围
public @interface ItcastAnnotation {

     String value() defailt "red";//这个是默认的值

     String color();

     String[] strArr();
}

使用的时候

@ItcastAnnotation(color=”red”,value=”“,strArr={“x”,”2”})
public class AnnotationTest {

public static void main(String[] args) {
    //检查某个注解是否存在
    boolean present = AnnotationTest.class.isAnnotationPresent(ItcastAnnotation.class);
    if(present){
        ItcastAnnotation annotation = AnnotationTest.class.getAnnotation(ItcastAnnotation.class);
        //获得上面的属性
        annotation.color();
        annotation.value();
        annotation.strArr();
    }
}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值