Python 官方文档:入门教程 => 点击学习
定义 1、如果注解中有属性,那么必须给属性赋值。 package com.lxc.Test; // 定义一个注解 public @interface Annota
1、如果注解中有属性,那么必须给属性赋值。
package com.lxc.Test;
// 定义一个注解
public @interface Annotation {
String name(); // 看似name像一个方法,实际上我们把name称为属性
}
使用上边注解:
package com.lxc.Test;
public class Test {
@Annotation(name="lxc")
public void test() {
}
}
2、如果注解中有属性,且没有定义默认值,那么在使用注解的时候,必须给属性赋值。
public @interface Annotation {
String name();
int age();
}
public class Test {
@Annotation(name="lxc", age=20)
public void test() {
}
}
但是注解中如果有默认值,在使用注解时,可以不给属性赋值
public class Test {
@Annotation(name="lxc")
public void test() {
}
}
public @interface Annotation {
String name();
String password() default "123";
}
3、value() 属性
如果注解中的一个属性名是value,且有且只有一个value(),在使用注解的时候,属性名可以省略
public class Test {
@Annotation("lxc")
public void test() {
}
}
public @interface Annotation {
String value();
String password() default "123";
}
注解中属性的类型有哪些
byte、short、int、float、double、boolean、char、String、Class、枚举
数组:
如果数组属性中有一个元素,那么数组的大括号可以省略:
public @interface Annotation {
String[] name();
}
public class Test {
// @Annotation(name={"lxc"}) // 写法一
@Annotation(name="lxc") // 写法二
public void test() {
}
}
枚举:
public enum MyEnum {
className, name, age,
}
public @interface Annotation {
String[] name();
MyEnum[] student();
}
public class Test {
@Annotation(name="lxc", student = {MyEnum.className, MyEnum.age})
public void test() {
}
}
注解如何使用:
(1)标记一个注解只能出现在类或者方法上
@Target(value = {ElementType.TYPE, ElementType.METHOD})
public @interface Annotation {
String userName();
MyEnum[] student();
}
(2)标记一个注解可以被反射机制所读取
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
// 标记注解只能出现在类上
@Target(value = {ElementType.TYPE, ElementType.METHOD})
// 标记注解可以被反射机制所读取
@Retention(RetentionPolicy.RUNTIME)
public @interface Annotation {
String userName();
MyEnum[] student();
}
通过反射机制来获取。
(1)获取类上边注解的属性:
注解类:
package com.lxc.Test;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
// 标记注解只能出现在类上
@Target(value = {ElementType.TYPE, ElementType.METHOD})
// 标记注解可以被反射机制所读取
@Retention(RetentionPolicy.RUNTIME)
public @interface Annotation {
String userName() default "吕星辰";
}
使用注解类:
// myAnnotation
@Annotation(userName = "哈哈")
public class myAnnotation {
}
获取注解类中 的属性:
package com.lxc.Test;
public class Test {
public static void main(String[] args) throws Exception{
Class c = Class.forName("com.lxc.Test.myAnnotation");
System.out.println(c.isAnnotationPresent(Annotation.class));
// 判断一个类是否有:Annotation这个注解
if(c.isAnnotationPresent(Annotation.class)) {
// 获取注解对象
Annotation ann = (Annotation) c.getAnnotation(Annotation.class);
// 通过注解对象获取属性值
System.out.println(ann.userName());
}
}
}
(2)获取类中方法上边注解的属性:
注解类:
package com.lxc.Test;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
// 标记注解只能出现在类上
@Target(value = {ElementType.TYPE, ElementType.METHOD})
// 标记注解可以被反射机制所读取
@Retention(RetentionPolicy.RUNTIME)
public @interface Annotation {
String userName();
String password();
}
在方法上使用注解及获取方法上边的注解:
分析:想获取方法上的注解,首先需要获取该方法,获取该方法的前提,获取该方法的类:
package com.lxc.Test;
import java.lang.reflect.Method;
public class UserAnnotation {
@Annotation(userName = "lxc", passWord = "123")
public void getUser() {}
public static void main(String[] args) throws Exception{
// 通过反射获取类
Class c = Class.forName("com.lxc.Test.UserAnnotation");
// 通过反射获取类中的方法
Method getUserMethod = c.getDeclaredMethod("getUser");
// 判断方法是否有 Annotation 这个注解
if(getUserMethod.isAnnotationPresent(Annotation.class)) {
Annotation ann = getUserMethod.getAnnotation(Annotation.class);
System.out.println(ann.userName()); // lxc
System.out.println(ann.password()); // 123
}
}
}
到此这篇关于详解java注解的使用的文章就介绍到这了,更多相关java注解内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: 详解java注解相关知识
本文链接: https://www.lsjlt.com/news/129143.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0