zoukankan      html  css  js  c++  java
  • 注解Annotation

    三个常用的注解

     

    @Override

    public class OverrideTest

    {

        //@Override表示子类要重写基类对应的方法,可以防止出现写错方法名的错误。

        @Override

        public String toString()

        {

           return "OverrideTest [toString()=" + super.toString() + "]";

        }

       

        public static void main(String[] args)

        {

           OverrideTest test = new OverrideTest();

          

           System.out.println(test);

        }

    }

     

    @Deprecated

    public class DeprecatedTest

    {

        //@Deprecated表示方法是不建议被使用的

        @Deprecated

        public void dosomthing()

        {

           System.out.println("do somthing");

        }

       

        public static void main(String[] args)

        {

           DeprecatedTest test = new DeprecatedTest();

           test.dosomthing();

        }

    }

     

     

    @SuppressWarnings

    public class SuppressWarningsTest

    {

        //表示抑制警告

        @SuppressWarnings({"unchecked","deprecation"})

        public static void main(String[] args)

        {

           Map map = new HashMap();

          

           map.put("hello",new Date());

          

           System.out.println(map);

          

           Date date = new Date();

          

           System.out.println(date.toLocaleString());

        }

    }

     

     

    自定义注解类型

    当我们用@interface关键字去定义一个注解时,该注解隐含地继承了java.lang.annotation.Annotation接口;如果我们定义了一个接口,并且让该接口继承自Annotation,那么我们所定义的依然是接口而不是注解,Annotation本身是接口而不是注解,可以与Enum类比.

    public @interface AnnotationTest

    {

        String value() default "anllin";

    }

     

    定义注解时,如果属性名为value,则在使用时,可以不用value="class"的形式赋值

    可以直接赋值。除value外其他都要用name = value的形式赋值

    @AnnotationTest("class")

    public class AnnotationUsage

    {

        @AnnotationTest("method")

        public static void method()

        {

           System.out.println("Hello World");

        }

       

        public static void main(String[] args)

        {

           method();

        }

    }

     

     

     

    @Retention的使用

    @Retention(RetentionPolicy.RUNTIME)

    public @interface MyAnnotation

    {

        String hello() default "com.anllin";

        String world();

    }

     

     

    @MyAnnotation(hello = "shenzhen",world = "futian")

    public class MyTest

    {

        @MyAnnotation(hello = "shanghai" , world = "shangpu")

        @Deprecated

        @SuppressWarnings("unchecked")

        public void output()

        {

           System.out.println("output");

        }

    }

     

     

     

     

    java.lang.reflect.AnnotatedElement接口

     

    反射中的Class,Constructor,Field,Mehtod,Package等类别,都实现了AnnotatedElemnet接口。

     

     

    使用反射获得注解

    只有@Retention(RetentionPolicy.RUNTIME)修饰的注解才可以用反射获得,标志发生在运行时。

     

    public class MyReflection

    {

        public static void main(String[] args) throws Exception

        {

           MyTest test = new MyTest();

          

           Class<MyTest> c = MyTest.class;

          

           Method method = c.getMethod("output",new Class[]{});

          

           if(method.isAnnotationPresent(MyAnnotation.class))

           {

               method.invoke(test, new Object[]{});

              

               MyAnnotation myAnnot = method.getAnnotation(MyAnnotation.class);

              

               String hello = myAnnot.hello();

               String world = myAnnot.world();

              

               System.out.println(hello + "," + world);

           }

          

           System.out.println("-----------------");

          

           Annotation[] annos = method.getAnnotations();

          

           for(Annotation an : annos)

           {

               System.out.println(an.annotationType().getName());

           }

        }

    }

     

     

     

    @TargetElementType的使用

    主要用于限定注解的使用范围

    @Target(ElementType.METHOD)

    public @interface MyTarget

    {

        String value() default "com.anllin";

    }

     

    public class MyTargetTest

    {

        @MyTarget("hello")

        public void deSomething()

        {

           System.out.println("hello,world");

        }

    }

     

     

     

    @Documented的使用

    用于使用者在制作JavaDoc文件时,一并将Annotation的讯息加入至API文件中。

    @Documented

    public @interface DocumentedAnnotation

    {

        String hello();

    }

     

    public class DocumentedTest

    {

        @DocumentedAnnotation(hello="welcome")

        public void method()

        {

           System.out.println("hello,world");

        }

    }

     

     

     

    @inherited

    预设上父类别中的Annotation并不会被继承至子类别中

     

     

     

     

     

    JUnit4的执行的一般流程:

    1.首先获得待测试类所对应的Class对象。

    2.然后通过该Class对象获得当前类中所有public方法所对应的Method数组。

    3.遍历该Method数组,取得每一个Method对象。

    4.调用每个Method对象的isAnnotationPresent(Test.Class)方法,判断该方法是否被Test注解所修饰。

    5,如果该方法返回true,那么调用method.invoke方法去执行该方法,否则不执行。

     三个常用的注解

     

    @Override

    public class OverrideTest

    {

        //@Override表示子类要重写基类对应的方法,可以防止出现写错方法名的错误。

        @Override

        public String toString()

        {

           return "OverrideTest [toString()=" + super.toString() + "]";

        }

       

        public static void main(String[] args)

        {

           OverrideTest test = new OverrideTest();

          

           System.out.println(test);

        }

    }

     

    @Deprecated

    public class DeprecatedTest

    {

        //@Deprecated表示方法是不建议被使用的

        @Deprecated

        public void dosomthing()

        {

           System.out.println("do somthing");

        }

       

        public static void main(String[] args)

        {

           DeprecatedTest test = new DeprecatedTest();

           test.dosomthing();

        }

    }

     

     

    @SuppressWarnings

    public class SuppressWarningsTest

    {

        //表示抑制警告

        @SuppressWarnings({"unchecked","deprecation"})

        public static void main(String[] args)

        {

           Map map = new HashMap();

          

           map.put("hello",new Date());

          

           System.out.println(map);

          

           Date date = new Date();

          

           System.out.println(date.toLocaleString());

        }

    }

     

     

    自定义注解类型

    当我们用@interface关键字去定义一个注解时,该注解隐含地继承了java.lang.annotation.Annotation接口;如果我们定义了一个接口,并且让该接口继承自Annotation,那么我们所定义的依然是接口而不是注解,Annotation本身是接口而不是注解,可以与Enum类比.

    public @interface AnnotationTest

    {

        String value() default "anllin";

    }

     

    定义注解时,如果属性名为value,则在使用时,可以不用value="class"的形式赋值

    可以直接赋值。除value外其他都要用name = value的形式赋值

    @AnnotationTest("class")

    public class AnnotationUsage

    {

        @AnnotationTest("method")

        public static void method()

        {

           System.out.println("Hello World");

        }

       

        public static void main(String[] args)

        {

           method();

        }

    }

     

     

     

    @Retention的使用

    @Retention(RetentionPolicy.RUNTIME)

    public @interface MyAnnotation

    {

        String hello() default "com.anllin";

        String world();

    }

     

     

    @MyAnnotation(hello = "shenzhen",world = "futian")

    public class MyTest

    {

        @MyAnnotation(hello = "shanghai" , world = "shangpu")

        @Deprecated

        @SuppressWarnings("unchecked")

        public void output()

        {

           System.out.println("output");

        }

    }

     

     

     

     

    java.lang.reflect.AnnotatedElement接口

     

    反射中的Class,Constructor,Field,Mehtod,Package等类别,都实现了AnnotatedElemnet接口。

     

     

    使用反射获得注解

    只有@Retention(RetentionPolicy.RUNTIME)修饰的注解才可以用反射获得,标志发生在运行时。

     

    public class MyReflection

    {

        public static void main(String[] args) throws Exception

        {

           MyTest test = new MyTest();

          

           Class<MyTest> c = MyTest.class;

          

           Method method = c.getMethod("output",new Class[]{});

          

           if(method.isAnnotationPresent(MyAnnotation.class))

           {

               method.invoke(test, new Object[]{});

              

               MyAnnotation myAnnot = method.getAnnotation(MyAnnotation.class);

              

               String hello = myAnnot.hello();

               String world = myAnnot.world();

              

               System.out.println(hello + "," + world);

           }

          

           System.out.println("-----------------");

          

           Annotation[] annos = method.getAnnotations();

          

           for(Annotation an : annos)

           {

               System.out.println(an.annotationType().getName());

           }

        }

    }

     

     

     

    @TargetElementType的使用

    主要用于限定注解的使用范围

    @Target(ElementType.METHOD)

    public @interface MyTarget

    {

        String value() default "com.anllin";

    }

     

    public class MyTargetTest

    {

        @MyTarget("hello")

        public void deSomething()

        {

           System.out.println("hello,world");

        }

    }

     

     

     

    @Documented的使用

    用于使用者在制作JavaDoc文件时,一并将Annotation的讯息加入至API文件中。

    @Documented

    public @interface DocumentedAnnotation

    {

        String hello();

    }

     

    public class DocumentedTest

    {

        @DocumentedAnnotation(hello="welcome")

        public void method()

        {

           System.out.println("hello,world");

        }

    }

     

     

     

    @inherited

    预设上父类别中的Annotation并不会被继承至子类别中

     

     

     

     

     

    JUnit4的执行的一般流程:

    1.首先获得待测试类所对应的Class对象。

    2.然后通过该Class对象获得当前类中所有public方法所对应的Method数组。

    3.遍历该Method数组,取得每一个Method对象。

    4.调用每个Method对象的isAnnotationPresent(Test.Class)方法,判断该方法是否被Test注解所修饰。

    5,如果该方法返回true,那么调用method.invoke方法去执行该方法,否则不执行。

     三个常用的注解

     

    @Override

    public class OverrideTest

    {

        //@Override表示子类要重写基类对应的方法,可以防止出现写错方法名的错误。

        @Override

        public String toString()

        {

           return "OverrideTest [toString()=" + super.toString() + "]";

        }

       

        public static void main(String[] args)

        {

           OverrideTest test = new OverrideTest();

          

           System.out.println(test);

        }

    }

     

    @Deprecated

    public class DeprecatedTest

    {

        //@Deprecated表示方法是不建议被使用的

        @Deprecated

        public void dosomthing()

        {

           System.out.println("do somthing");

        }

       

        public static void main(String[] args)

        {

           DeprecatedTest test = new DeprecatedTest();

           test.dosomthing();

        }

    }

     

     

    @SuppressWarnings

    public class SuppressWarningsTest

    {

        //表示抑制警告

        @SuppressWarnings({"unchecked","deprecation"})

        public static void main(String[] args)

        {

           Map map = new HashMap();

          

           map.put("hello",new Date());

          

           System.out.println(map);

          

           Date date = new Date();

          

           System.out.println(date.toLocaleString());

        }

    }

     

     

    自定义注解类型

    当我们用@interface关键字去定义一个注解时,该注解隐含地继承了java.lang.annotation.Annotation接口;如果我们定义了一个接口,并且让该接口继承自Annotation,那么我们所定义的依然是接口而不是注解,Annotation本身是接口而不是注解,可以与Enum类比.

    public @interface AnnotationTest

    {

        String value() default "anllin";

    }

     

    定义注解时,如果属性名为value,则在使用时,可以不用value="class"的形式赋值

    可以直接赋值。除value外其他都要用name = value的形式赋值

    @AnnotationTest("class")

    public class AnnotationUsage

    {

        @AnnotationTest("method")

        public static void method()

        {

           System.out.println("Hello World");

        }

       

        public static void main(String[] args)

        {

           method();

        }

    }

     

     

     

    @Retention的使用

    @Retention(RetentionPolicy.RUNTIME)

    public @interface MyAnnotation

    {

        String hello() default "com.anllin";

        String world();

    }

     

     

    @MyAnnotation(hello = "shenzhen",world = "futian")

    public class MyTest

    {

        @MyAnnotation(hello = "shanghai" , world = "shangpu")

        @Deprecated

        @SuppressWarnings("unchecked")

        public void output()

        {

           System.out.println("output");

        }

    }

     

     

     

     

    java.lang.reflect.AnnotatedElement接口

     

    反射中的Class,Constructor,Field,Mehtod,Package等类别,都实现了AnnotatedElemnet接口。

     

     

    使用反射获得注解

    只有@Retention(RetentionPolicy.RUNTIME)修饰的注解才可以用反射获得,标志发生在运行时。

     

    public class MyReflection

    {

        public static void main(String[] args) throws Exception

        {

           MyTest test = new MyTest();

          

           Class<MyTest> c = MyTest.class;

          

           Method method = c.getMethod("output",new Class[]{});

          

           if(method.isAnnotationPresent(MyAnnotation.class))

           {

               method.invoke(test, new Object[]{});

              

               MyAnnotation myAnnot = method.getAnnotation(MyAnnotation.class);

              

               String hello = myAnnot.hello();

               String world = myAnnot.world();

              

               System.out.println(hello + "," + world);

           }

          

           System.out.println("-----------------");

          

           Annotation[] annos = method.getAnnotations();

          

           for(Annotation an : annos)

           {

               System.out.println(an.annotationType().getName());

           }

        }

    }

     

     

     

    @TargetElementType的使用

    主要用于限定注解的使用范围

    @Target(ElementType.METHOD)

    public @interface MyTarget

    {

        String value() default "com.anllin";

    }

     

    public class MyTargetTest

    {

        @MyTarget("hello")

        public void deSomething()

        {

           System.out.println("hello,world");

        }

    }

     

     

     

    @Documented的使用

    用于使用者在制作JavaDoc文件时,一并将Annotation的讯息加入至API文件中。

    @Documented

    public @interface DocumentedAnnotation

    {

        String hello();

    }

     

    public class DocumentedTest

    {

        @DocumentedAnnotation(hello="welcome")

        public void method()

        {

           System.out.println("hello,world");

        }

    }

     

     

     

    @inherited

    预设上父类别中的Annotation并不会被继承至子类别中

     

     

     

     

     

    JUnit4的执行的一般流程:

    1.首先获得待测试类所对应的Class对象。

    2.然后通过该Class对象获得当前类中所有public方法所对应的Method数组。

    3.遍历该Method数组,取得每一个Method对象。

    4.调用每个Method对象的isAnnotationPresent(Test.Class)方法,判断该方法是否被Test注解所修饰。

    5,如果该方法返回true,那么调用method.invoke方法去执行该方法,否则不执行。

     

  • 相关阅读:
    MySQL基准测试--innodb_buffer_pool_instances
    MySQL参数优化:back_log
    MySQL open_files_limit相关设置
    Django权限系统auth模块详解
    2.9 go mod 之本地仓库搭建
    my40_MySQL锁概述之意向锁
    my39_InnoDB锁机制之Gap Lock、Next-Key Lock、Record Lock解析
    2.8 GO 参数传递
    my38_MySQL事务知识点零记
    my37_MGR流控对数据库性能的影响以及MGR与主从的性能对比
  • 原文地址:https://www.cnblogs.com/zfc2201/p/2143681.html
Copyright © 2011-2022 走看看