黑马程序员——Java注解(Annotation)

黑马程序员——Java注解(Annotation)

---------------------- android培训java培训、期待与您交流! ----------------------

一、Annotation概述

从Java5.0版发布以来,5.0平台提供了一个正式的Annotation功能:允许开发者定义、使用自己的Annotation类型。此功能由一个描述Annoation声明的语法,读取Annotation的API,一个使用Annotation修饰的class文件,一个Annotation处理工具(apt)组成。

Annotation并不直接影响代码语义,但是它能以工作的方式被看作类似程序的工具或者类库,它会反过来对正在运行的程序语义有所影响。Annotation可以从源文件、class文件或者以在运行的反射的多种方式被读取。

二、内置注解的使用

1.       SupressWarnings:关闭特定的警告信息。例如你在使用泛型的时候未指定类型。

示例一:

	@SuppressWarnings("deprecation")
         public static void main(String[] args){
                   System.runFinalizersOnExit(true);
         }

示例二:

@SuppressWarnings({"rawtypes", "unchecked" })
public staticvoid main(String[] args) {             
           Mapmap = new TreeMap();            
           map.put("hello",new Date());
           System.out.println(map.get("hello"));
}

2.      Deprecated:对不应再使用的方法进行注解。

public class DeprecatedTest {
    @Deprecated
    public void doSomething() {
        System.out.println("doSomething");
    }
 
    public static void main(String[] args) {
        DeprecatedTest test = new DeprecatedTest();
        test.doSomething();
    }
}

3.       Override:可以保证编译时Override函数的声明正确性。只能用于方法(不能用于类,包括声明或者其他结构)

示例:

public class OverrideTest {
    @Override
    public String toString(){
        return "This isOverride";
    }
   
    public static void main(String[] args) {
        OverrideTest test=new OverrideTest();
        System.out.println(test);
    }
}

三、自定义注解

1.      定义注解的示例一:

import java.lang.annotation.*;
public @interface MyAnnotation {
    String hello() default "fengyan";
    String world();
}

2.       定义注解时可以@Retention注解修饰,Retention这个元注释和java编译器处理注释的注释类型方式相关,告诉编译器在处理自定义注释类型的几种不同的选择,其值使用RetentionPolicy枚举类:

public enum RetentionPolicy{ 
    SOURCE, // 编译器处理完Annotation后不存储在class中 
    CLASS, // 编译器把Annotation存储在class中,这是默认值 
    RUNTIME // 编译器把Annotation存储在class中,可以由虚拟机读取,反射需要 
} 

         实例:

importjava.lang.annotation.*;
 
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
    String hello() default "Hello";
    String world();
}
 
@MyAnnotation(world = "World")
class MyAnnotationTest {
    public static void main(String[]args) {
        if (MyAnnotationTest.class.isAnnotationPresent(
MyAnnotation.class)) {
            MyAnnotation annotation =MyAnnotationTest.class
                    .getAnnotation(MyAnnotation.class);
            Stringhello=annotation.hello();
            Stringworld=annotation.world();
           
            System.out.println(hello);
            System.out.println(world);
        }
    }
}

3.      定义注解时可以用@Target修饰,它表示此定义的这个注解可修饰哪些元素(如类、方法、字段、局部变量、包等),@Target注解的属性值是一个ElementType枚举类型,它的源码如下:

public enum ElementType {
    /** Class, interface (including annotation type), or enumdeclaration */
    TYPE,
    /** Field declaration (includes enum constants) */
    FIELD,
    /** Method declaration */
    METHOD,
    /** Parameter declaration */
    PARAMETER,
    /** Constructor declaration */
    CONSTRUCTOR,
    /** Local variable declaration */
    LOCAL_VARIABLE,
    /** Annotation type declaration */
   ANNOTATION_TYPE,
    /** Package declaration */
    PACKAGE
}

四、注意事项: 

1.所有的Annotation自动继承java.lang.annotation接口 

2.自定义注释的成员变量访问类型只能是public、default;(所有的都能访问,源作者没用到函数:getDeclaredFields而已) 

3.成员变量的只能使用基本类型(byte、short、int、char、long、double、float、boolean和String、Enum、Class、annotations以及该类型的数据) 

4.如果只有一个成员变量,最好将参数名称设为value,赋值时不用制定名称而直接赋值

5.在实际应用中,还可以使用注释读取和设置Bean中的变量。

 

---------------------- android培训java培训、期待与您交流! ----------------------
详细请查看:http://edu.csdn.net/heima


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值