java中注解

来源:互联网 发布:苏宁售后知乎 编辑:程序博客网 时间:2024/06/14 16:46

Java注解(Annotation):

1:Override注解表示子类要重写父类的对应方法。

2:Deprecated注解表示方法不是建议被使用的。

3:SuppressWarnings表示抑制警告的意思。

4:自定义主键:当注解中的属性名为value时,在对其赋值是可以不指定属性名称

而直接写上属性的值即可。除了value以外的其他值都需要使用name=value这种赋值方式,即明确指定给谁赋值。

eg1:

package com;public @interface AnnotationTest { String value() default "hello"; String name();}

package com;@AnnotationTest(name="red")//使用默认值 value使用默认值public class AnnotationUsage {@AnnotationTest(value="baother", name = "sdfs")public void method () {}public static void main(String[] args) {@AnnotationTest(name = "sdf")int a = 0;}}

eg2:

package com;public @interface AnnotationTest { String value() default "hello"; EnumTest color();}

package com;@AnnotationTest(color=EnumTest.blue)//使用默认值 value使用默认值public class AnnotationUsage {@AnnotationTest(value="baother", color = EnumTest.pink)public void method () {}public static void main(String[] args) {@AnnotationTest(color = EnumTest.maroon)int a = 0;}}enum EnumTest {red,blue,pink,maroon;}


自定义Annocation类型

使用@interface自定义Annotation类型时,实际上是

自动继承了java.lang.annotation.Annotation接口,由编译程序自动

完成其它产生的细节。如果定义一个 接口,并且让该接口继承java.lang.annotation.Annotation,


该接口还是接口,不是注解。

java.lang.annotation.Annotation是个接口类型的而不是注解类型的。可以与enum类比。


5:告诉编译程序如何处理@Retention

java.lang.annotation.Retention型态可以在定义Annotation型态时,指示编译程序该如何对待你的自定义的Annotation型态。

缺省的情况下编译程序会将Annotation信息流在.class档案中,但不被虚拟机读取,而仅用在编译程序或工具程序运行时提供信息。

package com.inner;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;@Retention(value=RetentionPolicy.RUNTIME)public @interface MyAnnotation {String hello() default "zhangsai";String world();}package com.inner;@MyAnnotation(hello="shanghai",world="beijing")public class MyTest {@MyAnnotation(world="tiantang")@Deprecated//标记注解public void output () {System.out.println("output something");}}package com.inner;import java.lang.annotation.Annotation;import java.lang.reflect.Method;public class MyReflection {public static void main(String[] args) throws  Exception {Class<?> classType = MyTest.class;MyTest test = new MyTest ();Method method = classType.getMethod("output", new Class[]{});if (method.isAnnotationPresent(MyAnnotation.class)) {method.invoke(test, 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());}}}result:output somethingzhangsai:tiantanginterface com.inner.MyAnnotationinterface java.lang.Deprecated限定annotation使用对象@Targetpackage com.inner2;import java.lang.annotation.ElementType;import java.lang.annotation.Target;@Target({ElementType.METHOD})public @interface MyTarget {String value();}package com.inner2;public class MyTargetTest {@MyTarget("demo")public void method () {}}子类时候继承父类的@Inherited预设上父类别中的Annotation并不会被继承至子类别中可以在定义的Annotation型态时加上@Inherited


原创粉丝点击