java-自定义注解

1 package com.自定义注解;
2 @TestAnnotation(studentName = "高",id = 31,age = 20)
3 public class Test {
4     public static void main(String[] args) {
5 
6 
7     }
8 }
 1 import java.lang.annotation.ElementType;
 2 import java.lang.annotation.Retention;
 3 import java.lang.annotation.RetentionPolicy;
 4 import java.lang.annotation.Target;
 5 
 6 @Target(value = {ElementType.METHOD,ElementType.TYPE})
 7 @Retention(RetentionPolicy.RUNTIME)
 8 public @interface TestAnnotation {
 9 
10   String studentName() default "";
11   int age() default 0;
12   int id() default -1;
13 
14 }
package com.自定义注解;

public class Test02 {
    //注解中只有一个参数可以吧参数名省略
    @TestAnnotation02("aaa")
   public void test(){

   }
}
 1 package com.自定义注解;
 2 
 3 import java.lang.annotation.ElementType;
 4 import java.lang.annotation.Retention;
 5 import java.lang.annotation.RetentionPolicy;
 6 import java.lang.annotation.Target;
 7 
 8 @Target(value = {ElementType.METHOD,ElementType.TYPE})
 9 @Retention(RetentionPolicy.RUNTIME)
10 public @interface TestAnnotation02 {
11     String value();//注解里面只有一个参数就把他的参数名定为value.但是定义为别的也可以一般这么定义
12 }

 

posted @ 2019-06-13 16:27  七月的风没有雨  阅读(231)  评论(0编辑  收藏  举报