• 注解,自定义注解


    一、什么是注解

    二、自定义注解

    1. /**
    2. * 自定义注解
    3. */
    4. public @interface MyAnnotation {
    5. String aaa();
    6. boolean bbb() default true;
    7. String ccc();
    8. }
    9. @MyAnnotation ( aaa ="牛魔王",ccc = "sss")
    10. public class Test {
    11. @MyAnnotation ( aaa = "aaa",ccc = "666")
    12. public void test1(){
    13. }
    14. }

    三、注解的原理

    四、元注解

    1. import java.lang.annotation.ElementType;
    2. import java.lang.annotation.Retention;
    3. import java.lang.annotation.RetentionPolicy;
    4. import java.lang.annotation.Target;
    5. @Retention ( RetentionPolicy.RUNTIME )//控制下面的注解一直保留到执行时
    6. @Target ( {ElementType.TYPE,ElementType.METHOD} )//当前被修饰的注解只能用在类上
    7. public @interface MyTest3 {
    8. }

    五、注解的解析

    1. import java.lang.annotation.ElementType;
    2. import java.lang.annotation.Retention;
    3. import java.lang.annotation.RetentionPolicy;
    4. import java.lang.annotation.Target;
    5. @Retention ( RetentionPolicy.RUNTIME )
    6. @Target ( {ElementType.TYPE,ElementType.METHOD} )
    7. public @interface MyTest4 {
    8. String value();
    9. double aaa() default 100;
    10. String[] bbb();
    11. }
    12. public class Test4 {
    13. @Test
    14. //解析Demo类中的全部注解
    15. public void parseClass(){
    16. //1.先得到Class对象
    17. Class c = Demo.class;
    18. //2.解析类上的注解
    19. //判断一个类上是否有注解
    20. if (c.isAnnotationPresent ( MyTest4.class )){
    21. MyTest4 myTest4= (MyTest4) c.getDeclaredAnnotation ( MyTest4.class );
    22. System.out.println ( myTest4.value () );
    23. System.out.println ( myTest4.bbb () );
    24. }
    25. }
    26. }

    六、应用场景:模拟Junit框架

    1. import java.lang.annotation.ElementType;
    2. import java.lang.annotation.Retention;
    3. import java.lang.annotation.RetentionPolicy;
    4. import java.lang.annotation.Target;
    5. @Retention ( RetentionPolicy.RUNTIME )
    6. @Target ( ElementType.METHOD )
    7. public @interface MyTest {
    8. }
    9. import java.lang.reflect.InvocationTargetException;
    10. import java.lang.reflect.Method;
    11. public class Test2 {
    12. @MyTest
    13. public void test1(){
    14. System.out.println ("===========tes1========");
    15. }
    16. // @MyTest
    17. public void test2(){
    18. System.out.println ("===========tes2========");
    19. }
    20. // @MyTest
    21. public void test3(){
    22. System.out.println ("===========tes3========");
    23. }
    24. @MyTest
    25. public void test4(){
    26. System.out.println ("===========tes4========");
    27. }
    28. public static void main(String[] args) throws Exception {
    29. Test2 test = new Test2 ();
    30. //1.启动程序
    31. //得到Class对象
    32. Class c = Test2.class;
    33. //2.提取这个类中的全部成员方法
    34. Method[] methods = c.getDeclaredMethods ();
    35. for (Method method : methods) {
    36. if (method.isAnnotationPresent ( MyTest.class )) {
    37. //判断当前成员方法是否存在MyTest注解 如果存在 就触发执行
    38. method.invoke (test );
    39. }
    40. }
    41. }
    42. }

  • 相关阅读:
    (shorthand) pixelNeRF: Neural Radiance Fields from One or Few Images
    15.前端笔记-CSS-学成在线案例
    centos7安装WordPress
    [Flask]Pycharm+Flask零基础项目搭建入门
    lintcode 820 · 矩形【中等 vip 枚举法 数学】
    每日汇评:随着上升趋势的恢复,黄金在1950美元上方等待破位
    VUE3.0学习笔记
    【Python学习笔记】超详细Python快速入门教程(上)
    Cyanine5 Alkyne在生物分子标记与追踪中的应用
    第八天 Python爬虫之Rquests库&&打码平台的简单使用
  • 原文地址:https://blog.csdn.net/m0_64703222/article/details/133207980