package com.kuang.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
//自定义注解
public class Test03 {
//注解可以显式赋值,如果没有默认值,我们就必须给注解赋值
@MyAnnotation2(name = "吕伟康",schools = "清华大学")
public void test(){}
}
@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation2{
//注解的参数 : 参数类型 + 参数名();
String name();
int age() default 0;
int id() default -1;//如果默认值为-1,代表不存在。
String[] schools() default{"清华大学"};
}
@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation3{
String value();
}
注解Annotation外部注解
自定义注解实践:MyAnnotation2与MyAnnotation3在Java中的应用
最新推荐文章于 2024-04-29 19:12:57 发布
本文介绍了如何在Java中创建并使用自定义注解MyAnnotation2和MyAnnotation3,包括参数定义、显式赋值及在方法和类上的应用实例。重点展示了如何通过注解传递参数,以增强代码的可读性和元数据管理。
1万+

被折叠的 条评论
为什么被折叠?



