当前位置:编程学习 > JAVA >>

java中的注解

 import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import cn.itcast.day1.EnumTest;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.TYPE})
public @inte易做图ce ItcastAnnotation {
 String color() default "blue";
 String value();
 int[] arrayAttr() default {3,4,4};
 EnumTest.TrafficLamp lamp() default EnumTest.TrafficLamp.RED;
 MetaAnnotation annotationAttr() default @MetaAnnotation("lhm");
 Class classAttr() default String.class;
}


----------------------------------------------

import java.lang.reflect.Method;

//import javax.jws.soap.InitParam;

@ItcastAnnotation(classAttr=Integer.class,annotationAttr=@MetaAnnotation("flx"),color="red",value="abc",arrayAttr=1)
public class AnnotationTest {

 /**
  * @param args
  */
 @SuppressWarnings("deprecation")
 @ItcastAnnotation("xyz")
 public static void main(String[] args) throws Exception{
  // TODO Auto-generated method stub
  System.runFinalizersOnExit(true);
  if(AnnotationTest.class.isAnnotationPresent(ItcastAnnotation.class)){
   ItcastAnnotation annotation = (ItcastAnnotation)AnnotationTest.class.getAnnotation(ItcastAnnotation.class);
   System.out.println(annotation.color());
   System.out.println(annotation.value());
   System.out.println(annotation.arrayAttr()/*.length*/);
   System.out.println(annotation.lamp().nextLamp().name());
   System.out.println(annotation.annotationAttr().value());
   System.out.println(annotation.classAttr());
  }
 
  Method mainMethod = AnnotationTest.class.getMethod("main", String[].class);
  ItcastAnnotation annotation2 = (ItcastAnnotation)mainMethod.getAnnotation(ItcastAnnotation.class);
  System.out.println(annotation2.value());
 }

 @Deprecated
 public static void sayHello(){
  System.out.println("hi,���Dz���");
 }
}

-------------------------------------

public @inte易做图ce MetaAnnotation {
 String value();
}


 

 

补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,