java中注解

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 something
zhangsai:tiantang
interface com.inner.MyAnnotation
interface java.lang.Deprecated

限定annotation使用对象@Target
package 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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值