• 自定义注解-例子


    1.注解类

    package Annotations;
    
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface ClassMethodAnnotation {
    
        Class c();
        String method();
    }

    2.注解使用

    package Annotations;
    
    public class test {
    
        @ClassMethodAnnotation(c=test.class,method="test1")
        public void test(){
            System.out.println("===test方法执行了!!!!!");
        }
        
        
        public void test1(){
            System.out.println("test1方法执行了!!!!!");
        }
    }

    3.测试

    package Annotations;
    
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    
    public class AnnotationMainTest {
    
        public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException {
            ClassMethodAnnotationTest();
        }
    
        
        public static void ClassMethodAnnotationTest() throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException{
            Class testClass = test.class;
            Method[] method = testClass.getMethods();
            for (Method method2 : method) {
                if(method2.isAnnotationPresent(ClassMethodAnnotation.class)){
                    ClassMethodAnnotation annotation = method2.getAnnotation(ClassMethodAnnotation.class);
                    Class annClass = annotation.c();
                    String annMethod = annotation.method();
                    Method[] anmethod = annClass.getMethods();
                    for (Method method3 : anmethod) {
                        if(method3.getName().equals(annMethod)){
                            System.out.println("带注解的方法为:"+method2.getName());
                            method3.invoke(annClass.newInstance(), null);
                        }
                    }
                }
            }
        }
    }

    执行结果:

  • 相关阅读:
    50.Ext_数字输入框_Ext.form.NumberField
    49.Ext.form.TextField()基本用法
    48.EXt.Data.JsonReader()
    47. Ext.form.Field.prototype.msgTarget
    46. Ext中namespace的作用(转)
    45. ExtJS ComboBox 下拉列表详细用法
    44. Ext信息提示对话框
    43. ExtJs控件属性配置详细
    42.extjs Combobox动态加载数据问题,mode:local 还是remote
    堆叠顺序
  • 原文地址:https://www.cnblogs.com/chinazhou-wang/p/11573380.html
Copyright © 2020-2023  润新知