zoukankan      html  css  js  c++  java
  • Java 注解 Annotation

    一、Annotation工作方式

    从Java5.0版本发布以来,5.0平台提供了一个正式的annotation功能:允许开发者定义、使用自己的annotation类型。

    此功能由一个定义annotation类型的语法和一个描述annotation声明的语法,读取annotation的API,一个使用annotation

    修饰的class文件,一个annotation处理工具(apt)组成。

    annotation并不直接影响代码语义,但是它能够工作的方式被看做类似程序的工具或者类库

    它会反过来对正在运行的程序语义有所影响。 annotation可以从原文件、class文件或者以运行时反射的多种方式读取。

    二、Java注解

    1、Override注解

        表示子类要重写(override)父类的对应方法

        java.lang.Override是个Marker annotation,用户标识的Annotation,Annotation名称本身即表示了要给工具程序的信息

    2、 Deprecated注解

        表示方法是不建议被使用的。

    3、SuppressWarning注解

      表示抑制警告

    三、自定义注解

    1.定义一个注解

    public @interface AnnotationTest {
    	//属性value
    	String value();
    }
    

      

    使用注解

    @AnnotationTest("hello")
    public class AnnotationUsage {
    	
    	@AnnotationTest("world")
    	public void method() {
    		System.out.println("usage of annotation");
    	}
    	
    	public static void main(String[] args) {
    		AnnotationUsage usage = new AnnotationUsage();
    		usage.method();
    	}
    }
    

      

    2、 给注解增加默认值和枚举值

    public @interface AnnotationTest {
    	//属性value,default设置默认值
    	String value() default "hello";
    	EnumTest value2();
    }
    
    enum EnumTest{
    	Hello,
    	World,
    	Welcome;
    }
    

      使用注解

    @AnnotationTest(value2 = EnumTest.Welcome)
    public class AnnotationUsage {
    	
    	@AnnotationTest(value="world", value2 = EnumTest.World)
    	public void method() {
    		System.out.println("usage of annotation");
    	}
    	
    	public static void main(String[] args) {
    		AnnotationUsage usage = new AnnotationUsage();
    		usage.method();
    	}
    }
    

      

    三、注解的调用

    1、创建注解

    @Retention(RetentionPolicy.RUNTIME)
    public @interface MyAnnotation {
    	
    	String hello() default "tom";
    	
    	String world();
    }
    

      

    2、在类和方法中使用注解

    @MyAnnotation(hello="beijing", world="shanghai")
    public class MyTest {
    	
    	@MyAnnotation(hello="hangzhou", world="xihu")
    	@Deprecated
    	@SuppressWarnings("unchecked")
    	public void output() {
    		System.out.println("output something");
    	}
    }
    

      

    3、使用反射测试

    public class MyReflection {
    	
    	public static void main(String[] args) throws Exception {
    		MyTest myTest = new MyTest();
    		Class<MyTest> c = MyTest.class;
    		Method method = c.getMethod("output", new Class[]{});
    		if(method.isAnnotationPresent(MyAnnotation.class)){
    			
    			method.invoke(myTest, new Object[]{});
    			MyAnnotation myAnnotation = method.getAnnotation(MyAnnotation.class);
    			String hello = myAnnotation.hello();
    			String world = myAnnotation.world();
    			System.out.println(hello + ", " + world);
    		}
    		
    		Annotation[] annotations = method.getAnnotations();
    		for(Annotation annotation : annotations){
    			System.out.println(annotation.annotationType().getName());
    		}
    	}
    }
    

      输出结果

    output something
    hangzhou, xihu
    com.example.annotation.MyAnnotation
    java.lang.Deprecated
    

      

     四、Target的使用

    1、定义一个注解,target为方法,只能被方法使用

    @Target(ElementType.METHOD)
    public @interface MyTarget {
    	String value();
    }
    

      

    2.使用

    如上图,类中使用该注解就会报错。

  • 相关阅读:
    git 常用命令
    mac 显示隐藏文件
    android 图片缓存
    字符串与枚举相互转换
    ios 消息通知
    ios 真机调试
    ios 宏定义 系统版本 判定
    autolayout autoresizing
    c++对象创建带括号与无括号的区别
    内存对齐
  • 原文地址:https://www.cnblogs.com/linlf03/p/9871047.html
Copyright © 2011-2022 走看看