java注解,在继承时会被子类覆盖,会被子类覆盖的函数覆盖,如果继承的子类不写注解的话,默认没有注解,即不会继承父类的注解

本文通过一个Java程序示例展示了如何使用注解来为方法添加元数据,并通过反射读取这些注解信息。具体包括定义自定义注解类型、将注解应用于方法上以及在运行时获取并打印注解中的属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package Test;

import java.lang.reflect.Method;

@person2(Integer_value = 0, b = 0)
public class Annotation {
	@person(Integer_value = 12, b = 12, value = "method1")
	public void method1(){
		
	}
	@person(Integer_value = 24, b = 123, value = "method2")
	public void method2(){
		
	}
	@person(Integer_value = 0, b = 0,value ="method3")
	public void method3(){
		
	}
	public static void main(String[] args){
		// TODO Auto-generated method stub
		Class c=Annotation.class;
		Method[] m=c.getMethods();
		for(Method t:m){
			if(t.isAnnotationPresent(person.class)){
			person name=t.getAnnotation(person.class);
			System.out.println(name.value());
			System.out.println(name.b());
			System.out.println(name.Integer_value());
			
			}
		}
		Class cc=Annotation.class;

		System.out.println(c.getAnnotation(person2.class));
		class ext extends Annotation{
			
			public void method3(){
				
			}
		}
		Class e=ext.class;
		Method[] me=e.getMethods();
		for(Method t:me){
			if(t.isAnnotationPresent(person.class)){
			person name=t.getAnnotation(person.class);
			System.out.println(name.value());
			System.out.println(name.b());
			System.out.println(name.Integer_value());
			}
		}
	}
	
}

输出结果

method1
12
12
method2
123
24
method3
0
0
@Test.person2(value=def, Integer_value=0, b=0)
method1
12
12
method2
123
24





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值