高薪笔记---注解

 

注解:注解是用来告诉编译器,像它传递某种信息,作为一个标记。

      一个注解就是一个类,使用注解就相当于创建了类的对象

@SuppressWarnings("deprecation")要在特定的方法中取消显示某个警告,则应该注释该方法而不是注释它的类。 

@Deprecated   //注解说明该方法已过时

@Override //覆盖

@Deprecated   //过时

public static void sayHello(){

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

}

@SuppressWarnings("deprecation")

public static void main(String[] args) {

// TODO Auto-generated method stub

AnnotationTest.sayHello();

}

@Override

public String toString() {

return "TestDemo []";

}

package cn.itcast.annotation.demo;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

/*

 * 写一个注解类

 */

//元注解

@Retention(RetentionPolicy.RUNTIME)

public @interface ItcastAnnotation{

}

package cn.itcast.annotation.demo;

@ItcastAnnotation

public class AnnotationTest {

     // 

/**

 * @param args

 */

public static void main(String[] args) {

// TODO Auto-generated method stub

//检查该类是否调用了注解类

if(AnnotationTest.class.isAnnotationPresent(ItcastAnnotation.class)){

//使用反射获取到ItcastAnnotationDemo类对象

ItcastAnnotation annotation = (ItcastAnnotation)AnnotationTest.class.getAnnotation(ItcastAnnotation.class);

System.out.println(annotation);

}

}

@Deprecated

public static void sayHello() {

// TODO Auto-generated method stub

}

}

说明:

@Retention(RetentionPolicy.RUNTIME)  指示注释类型的注释要保留多久。默认是CLASS

CLASS
编译器将把注释记录在类文件中,但在运行时 JVM 不需要保留注释。

RUNTIME
编译器将把注释记录在类文件中,在运行时 JVM 将保留注释,因此可以反射性地读取。

SOURCE
编译器要丢弃的注释。



以下资料属于网上查找补充:

  下面是一个定义注解的实例

 

Java代码   收藏代码
  1. package Test_annotation;  
  2.   
  3. import java.lang.annotation.Documented;  
  4. import java.lang.annotation.Inherited;  
  5. import java.lang.annotation.Retention;  
  6. import java.lang.annotation.Target;  
  7. import java.lang.annotation.ElementType;  
  8. import java.lang.annotation.RetentionPolicy;  
  9.   
  10. /* 
  11.  * 元注解@Target,@Retention,@Documented,@Inherited 
  12.  *  
  13.  *     @Target 表示该注解用于什么地方,可能的 ElemenetType 参数包括: 
  14.  *         ElemenetType.CONSTRUCTOR 构造器声明 
  15.  *         ElemenetType.FIELD 域声明(包括 enum 实例) 
  16.  *         ElemenetType.LOCAL_VARIABLE 局部变量声明 
  17.  *         ElemenetType.METHOD 方法声明 
  18.  *         ElemenetType.PACKAGE 包声明 
  19.  *         ElemenetType.PARAMETER 参数声明 
  20.  *         ElemenetType.TYPE 类,接口(包括注解类型)或enum声明 
  21.  *          
  22.  *     @Retention 表示在什么级别保存该注解信息。可选的 RetentionPolicy 参数包括: 
  23.  *         RetentionPolicy.SOURCE 注解将被编译器丢弃 
  24.  *         RetentionPolicy.CLASS 注解在class文件中可用,但会被VM丢弃 
  25.  *         RetentionPolicy.RUNTIME VM将在运行期也保留注释,因此可以通过反射机制读取注解的信息。 
  26.  *          
  27.  *     @Documented 将此注解包含在 javadoc 中 
  28.  *      
  29.  *     @Inherited 允许子类继承父类中的注解 
  30.  *    
  31.  */  
  32. @Target(ElementType.METHOD)  
  33. @Retention(RetentionPolicy.RUNTIME)  
  34. @Documented  
  35. @Inherited  
  36. /* 
  37.  * 定义注解 Test 
  38.  * 注解中含有两个元素 id 和 description 
  39.  * description 元素 有默认值 "no description" 
  40.  */  
  41. public @interface Test {  
  42.     public int id();  
  43.     public String description() default "no description";  
  44. }  
   

下面是一个使用注解 和 解析注解的实例

 

Java代码   收藏代码
  1. package Test_annotation;  
  2.   
  3. import java.lang.reflect.Method;  
  4.   
  5. public class Test_1 {  
  6.     /* 
  7.      * 被注解的三个方法 
  8.      */  
  9.     @Test(id = 1, description = "hello method_1")  
  10.     public void method_1() {  
  11.     }  
  12.   
  13.     @Test(id = 2)  
  14.     public void method_2() {  
  15.     }  
  16.   
  17.     @Test(id = 3, description = "last method")  
  18.     public void method_3() {  
  19.     }  
  20.   
  21.     /* 
  22.      * 解析注解,将Test_1类 所有被注解方法 的信息打印出来 
  23.      */  
  24.     public static void main(String[] args) {  
  25.         Method[] methods = Test_1.class.getDeclaredMethods();  
  26.         for (Method method : methods) {  
  27.             /* 
  28.              * 判断方法中是否有指定注解类型的注解 
  29.              */  
  30.             boolean hasAnnotation = method.isAnnotationPresent(Test.class);  
  31.             if (hasAnnotation) {  
  32.                 /* 
  33.                  * 根据注解类型返回方法的指定类型注解 
  34.                  */  
  35.                 Test annotation = method.getAnnotation(Test.class);  
  36.                 System.out.println("Test( method = " + method.getName()  
  37.                         + " , id = " + annotation.id() + " , description = "  
  38.                         + annotation.description() + " )");  
  39.             }  
  40.         }  
  41.     }  
  42.   
  43. }  
   
输出结果如下:

     Test( method = method_1 , id = 1 , description = hello method_1 )
    Test( method = method_2 , id = 2 , description = no description )
    Test( method = method_3 , id = 3 , description = last method )

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值