这篇文章将为大家详细讲解有关java中注解的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
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;
/**
* c.isAnnotationPresent(注解类.class) : 判断一个类上是否有注解,返回true、false
* c.getAnnotation(注解类.class) : 获取注解类的实例
*
*/
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中注解的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。