美文网首页
java注解

java注解

作者: 稀饭粥95 | 来源:发表于2018-09-03 23:19 被阅读8次

    使用

    定义注解

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

    使用注解

    public class Main { 
        @Person(name = "123")
        public void print(){
            
        }
        
        public static void main(String[] args) {
            
            Class cl = Main.class;
            try {
                Method m = cl.getMethod("print",new Class[]{});
                Person p = m.getAnnotation(Person.class);
                System.out.println(p.name());
            } catch (NoSuchMethodException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SecurityException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        }
    
    }
    

    反编译

    反编译MyAnnotation.class

    public class MyAnnotation
    {
        public static interface Person
            extends Annotation
        {
    
            public abstract String name();
        }
    
    
        public MyAnnotation()
        {
        }
    }
    

    反编译注解MyAnnotation$Person.class

    public static interface MyAnnotation$Person
        extends Annotation
    {
    
        public abstract String name();
    }
    

    相关文章

      网友评论

          本文标题:java注解

          本文链接:https://www.haomeiwen.com/subject/igrwwftx.html