关于java 注解中元注解Inherited的使用详解

关于java中元注解Inherited 的使用说明

首先解释下元注解,就是用来中声明注解类型时需要使用到的注解。

Inherited作用是,使用此注解声明出来的自定义注解,在使用此自定义注解时,如果注解在类上面时,子类会自动继承此注解,否则的话,子类不会继承此注解。这里一定要记住,使用Inherited声明出来的注解,只有在类上使用时才会有效,对方法,属性等其他无效。

下面看下代码就明了了。

/**
 * 声明的此注解使用了Inherited元注解,表示此注解用在类上时,会被子类所继承
 * @author crazy
 */
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface InheritedTest {

	String value();
}

/**
 * 声明的此注解没有使用Inherited元注解,表示此注解用在类上时,不会被子类所继承
 * @author crazy
 */
@Retention(RetentionPolicy.RUNTIME)
public @interface InheritedTest2 {

	String value();
}
/**
 * 父类
 * @author crazy
 */
@InheritedTest("使用Inherited的注解 class")
@InheritedTest2("未使用Inherited的注解 class")
public class Parent {

	@InheritedTest("使用Inherited的注解 method")
	@InheritedTest2("未使用Inherited的注解 method")
	public void method(){
		
	}
	@InheritedTest("使用Inherited的注解 method2")
	@InheritedTest2("未使用Inherited的注解 method2")
	public void method2(){
		
	}
	
	@InheritedTest("使用Inherited的注解 field")
	@InheritedTest2("未使用Inherited的注解 field")
	public String a;
}

/**
 * 子类  只继承了一个method方法
 * @author crazy
 */
public class Child extends Parent {

	@Override
	public void method() {
	}
}

/**
 * 通过反射进行测试
 * @author crazy
 */
public class test {

	public static void main(String[] args) throws NoSuchMethodException, SecurityException, NoSuchFieldException {
		Class<Child> clazz = Child.class;
		//对类进行测试 
		System.out.println("对类进行测试");
		if(clazz.isAnnotationPresent(InheritedTest.class)){
			System.out.println(clazz.getAnnotation(InheritedTest.class).value());
		}
		if(clazz.isAnnotationPresent(InheritedTest2.class)){
			System.out.println(clazz.getAnnotation(InheritedTest2.class).value());
		}
		System.out.println();
		//对方法 进行测试
		System.out.println("对方法进行测试");
		Method method = clazz.getMethod("method", null);
		if(method.isAnnotationPresent(InheritedTest.class)){
			System.out.println(method.getAnnotation(InheritedTest.class).value());
		}
		if(method.isAnnotationPresent(InheritedTest2.class)){
			System.out.println(method.getAnnotation(InheritedTest2.class).value());
		}
		System.out.println();
		//对方法2 进行测试
		System.out.println("对方法2进行测试");
		Method method2 = clazz.getMethod("method2", null);
		if(method2.isAnnotationPresent(InheritedTest.class)){
			System.out.println(method2.getAnnotation(InheritedTest.class).value());
		}
		if(method2.isAnnotationPresent(InheritedTest2.class)){
			System.out.println(method2.getAnnotation(InheritedTest2.class).value());
		}
		System.out.println();
		//对属性测试
		System.out.println("对属性进行测试");
		Field field = clazz.getField("a");
		if(field.isAnnotationPresent(InheritedTest.class)){
			System.out.println(field.getAnnotation(InheritedTest.class).value());
		}
		if(field.isAnnotationPresent(InheritedTest2.class)){
			System.out.println(field.getAnnotation(InheritedTest2.class).value());
		}
	}
}

下面是输出结果

对类进行测试
使用Inherited的注解 class

对方法进行测试

对方法2进行测试
使用Inherited的注解 method2
未使用Inherited的注解 method2

对属性进行测试
使用Inherited的注解 field
未使用Inherited的注解 field

由上可以看出,通过Inherited元注解声明的自定义注解,在类上使用时,可以被子类继承,对第一个方法进行测试时,由于子类继承了父类方法,且两个都没有输出,证明Inherited对方法无效,由方法2可以看出,因为子类没有重写父类方法,所以是直接使用的父类方法,所以两个都会输出,同理属性也是,都会输出。

所以证明最后结论:通过对注解上使用元注解Inherited声明出的注解,在使用时用在类上,可以被子类所继承,对属性或方法无效。

  • 28
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 11
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值