Java annotation enum Demo

本文介绍了一个具体的Java应用案例,包括枚举类型的使用及自定义注解的实现方式。通过枚举类定义性别类型,并在注解中引用这些枚举值。此外,还展示了如何通过反射读取方法上的注解信息。

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

枚举类:

package com.sharp.shiro.vo;

public enum Sex{
	MALE("man"), FEMALE("woman");
	String age;
	private Sex(String age){
		this.age = age;
	}
}

annotation类:

package com.sharp.shiro.vo;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 自定义Annotation
 * @author WalkingDog
 *
 */

@Target(value = {ElementType.TYPE, ElementType.FIELD,
		ElementType.METHOD, ElementType.PARAMETER})

@Retention(RetentionPolicy.RUNTIME)
public @interface Many {
	String name();
	Sex sex();
}


操作类:

/**===========================================
 *        Copyright (C) 2013 Tempus
 *           All rights reserved
 *
 *  项 目 名: shiroDemo
 *  文 件 名: TestMain.java
 *  版本信息: V1.0.0 
 *  作    者: peng.xiao
 *  日    期: 2013-1-18-下午12:54:23
 * 
 ============================================*/

package com.sharp.shiro.vo;

import java.lang.reflect.Method;

/**
 * 类 名 称: TestMain
 * 类 描 述: 
 * 创 建 人: peng.xiao
 * 创建时间: 2013-1-18 下午12:54:23
 *
 * 修 改 人: peng.xiao
 * 操作时间: 2013-1-18 下午12:54:23
 * 操作原因: 
 * 
 */
public class TestMain
{
	@Many(name="xiaopeng",sex=Sex.FEMALE)
	public void testadd(){
		
	}
	public static void main(String[] args) throws Exception
	{
		Method method = new TestMain().getClass().getDeclaredMethod("testadd");
		System.out.println(method.isAnnotationPresent(Many.class));
		Many annotation = method.getAnnotation(Many.class);
		System.out.println(annotation.name());
		System.out.println(annotation.sex().age);
	}
}

打印结果:

true
xiaopeng
woman


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值