注释

一、注解的概念

一般情况不会让你去开发注解,但是在以后的框架中会大量使用注解,当你看到注解的时候不要觉得他是什么神奇的东西,注解的主要作用是对代码进行跟踪、警告等等,尤其是在容器中使用广泛(在容器中就使用了反射操作注解实现某些功能,比如说Spring容器中就大量的使用了注解),还可以使用反射操作注解,看看几个Java定义出的几个注解。
DEMO:@Override注解

 @Override//表示该方法是覆父类的方法,所以方法的形式要和父类保持一致
public String toString() {
    return super.toString();
}

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override {
}

·定义注解的方式:public @interface 注解名称
·@Target(ElementType.METHOD):表示注解的可以在指定的位置使用,ElementType.METHOD
注解可以在方法上使用
·@Retention(RetentionPolicy.SOURCE):指定了注解的生命周期,SOURCE表示注解是在源码的时候生效,当程序运行的时候就不生效了,这个效果后面我们用反射来证明
DEMO:@SuppressWarnings(“serial”)注解

@SuppressWarnings("serial")
public class Emp  implements Serializable {
}

该注解的本身表示压制警告。

@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
@Retention(RetentionPolicy.SOURCE)
public @interface SuppressWarnings {
    /**
     * The set of warnings that are to be suppressed by the compiler in the
     * annotated element.  Duplicate names are permitted.  The second and
     * successive occurrences of a name are ignored.  The presence of
     * unrecognized warning names is <i>not</i> an error: Compilers must
     * ignore any warning names they do not recognize.  They are, however,
     * free to emit a warning if an annotation contains an unrecognized
     * warning name.
     *
     * <p> The string {@code "unchecked"} is used to suppress
     * unchecked warnings. Compiler vendors should document the
     * additional warning names they support in conjunction with this
     * annotation type. They are encouraged to cooperate to ensure
     * that the same names work across multiple compilers.
     * @return the set of warnings to be suppressed
     */
    String[] value();
}

·@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
表示该注解可以在:类上、属性,参数、构造方法、局部变量
·@Retention(RetentionPolicy.SOURCE):注解的生命周期是在源码有效
DEMO: @Deprecated注解

public   void  add(){
      Date date=new Date();
      date.getYear();
}
@Deprecated
public int getYear() {
    return normalize().getYear() - 1900;
}

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE})
public @interface Deprecated {
}
·@Documented:表示如果对源码进行打包成api文档,那么注解的内容也会显示到api文档中

·@Retention(RetentionPolicy.RUNTIME):指定的注解的生命周期是到运行时,后面使用反射证明
·@Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE})
注解的使用范围。
元注解有如下四种:
@Documented –注解是否将包含在JavaDoc中,表示是否将注解
信息添加在java文档中
@Retention–什么时候使用该注解(也就是注解的生命周期)
@Target–指定注解的只用范围
@Inherited–是否允许子类继承该注解
元注解就是用来定义注解的注解

自定义注解

我们看了压制警告(@SuppressWarnings)、覆写(@Override)、过期声明(@Deprecated)
上面的注解是java定义好的,使用元注解定义的,我们也可以使用元注解定义自己的注解。
DEMO:自定义注解

 package com.sun.annotation;
import java.lang.annotation.*;
@Target({ElementType.METHOD,ElementType.FIELD,ElementType.TYPE,ElementType.LOCAL_VARIABLE})
//指定生命周期
@Retention(RetentionPolicy.RUNTIME)
//表示可以被子类继承
@Inherited
public @interface EmpAnotaion {
     String    empno()  default   "10001";
     String   ename()  default   "smith";
}

package com.sun;
import com.sun.annotation.EmpAnotaion;
import java.io.Serializable;
@EmpAnotaion
public class Emp  implements Serializable {
    @EmpAnotaion
    public   void  add(){
    }
}

使用反射读取注解

public class Test {
    public static void main(String[] args) throws Exception {
        //取得堆区中的Class<T>对象
        Class<?> classObj = Emp.class;
        //使用Class对象操作Emp类的注解
        Annotation annotation = classObj.getAnnotation(SuppressWarnings.class);
        System.out.println(annotation);
        //取得方法上的注解
        //1、取得方法对象
        Method method = classObj.getMethod("add");
        //取得方法上的注解
        Annotation mannotation = method.getAnnotation(EmpAnotaion.class);
        System.out.println(mannotation);
    }
}

在这里插入图片描述
压制警告的注解没有取得,原因是压制警告的注解的生命周期是在源码级别,EmpAnnotation注解的生命周期是Runtime
DEMO:读取自定义的注解

package com.sun;
import com.sun.annotation.EmpAnotaion;
import java.io.Serializable;
@SuppressWarnings("serial")
public class Emp  implements Serializable {
    @EmpAnotaion(empno = "1003")
    public   void  add(){
    }
}
public class Test {
    public static void main(String[] args) throws Exception {
        //取得堆区中的Class<T>对象
        Class<?> classObj = Emp.class;
        Method method = classObj.getMethod("add");
        //取得方法上的注解
        EmpAnotaion mannotation = method.getAnnotation(EmpAnotaion.class);
        System.out.println("雇员的编号是:"+ mannotation.empno());
        System.out.println("雇员的姓名是:"+ mannotation.ename());
    }
}

在这里插入图片描述
如果再注解中给出了注解元素的属性的默认值,则在使用注解的时候可以不给出具体的值,如果定义了属性但是没有给出默认值,则在使用注解的时候必须给出默认值。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值