您的位置:首页 > 编程语言 > Java开发

java 自定义注解使用

2016-01-18 17:10 609 查看
自定义注解

import java.lang.annotation.*;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface  MyAnnotation {
String method_name();
}


import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;

public class Test {

@MyAnnotation(method_name="testAnnotation1")
public String testAnnotation1(){
return "Hello, World!";
}

@MyAnnotation(method_name="testAnnotation2")
public String testAnnotation2(){
return "Hello, World!";
}

public String testAnnotation3(){
return "Hello, World!";
}

public static void main(String[] args) throws UnsupportedEncodingException {
Method[] test_methods = Test.class.getMethods();

for(Method m : test_methods){
if(m.isAnnotationPresent(MyAnnotation.class)){
MyAnnotation test_annotation = m.getAnnotation(MyAnnotation.class);
System.out.println(test_annotation.method_name());
}
}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: