现在的位置: 首页 > 综合 > 正文

Java annotation enum Demo

2017年12月26日 ⁄ 综合 ⁄ 共 1310字 ⁄ 字号 评论关闭

枚举类:

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

抱歉!评论已关闭.