Three, Java annotations

annotation

  • The concept: description of the process.

  • Definition: Notes (Annotation), also known as metadata. Describe a code level. It is a feature of JDK1.5 later introduced the classes, interfaces, enumerations at the same level. It can be declared in front of the packages, classes, fields, methods, local variables, the parameters of the method for these elements will be described, the comment.

  • Functional classification:

    • Documenting: metadata generated by the document identification code
    • Code analysis: analysis of the code by the code identified metadata
    • Compile_check: metadata identified by the code so the compiler can compile basic inspection
  • JDK predefined number of notes

    • @Override: annotation method for detecting whether the label inherited from the parent class
    • @Deprecated: This comment marked content, expressed obsolete
    • @SuppressWarnings: suppress warnings
  • Custom annotation

    • format:
      • Yuan notes
      • {} public @interface annotation name
    • Nature: an interface
    • Properties: Interface members can be defined method
      • Claim:
        • Property return type
          • Basic data types
          • String
          • enumerate
          • annotation
          • Above type array
        • Defines the properties, we need to use assignment
          • If the definition of property, use the default keyword to attribute the default initialization value, when using annotations, you can not assign attributes.
          • If you only need to assign a property, and the name attribute is value, the value may be omitted, can be defined directly.
          • When using the array assignment, the value of the {} package, if there is only one array only, can be omitted {}
    • Annotations are used to describe annotation: a meta-annotation
      • @Target: annotation can describe the role of location
        • ElementType Value
          • TYPE: can be at class
          • METHOD: the method can be applied to
          • FIELD: can act on the member variables
      • @Retention: Description stage annotations are reserved
        • RetentionPolicy Value
          • RUNTIME: current annotation to be described, will be retained to the class bytecode files, and read into the JVM
      • @Documented: description notes whether the document is drawn into the api
      • @Inherited: description notes whether inherited by subclasses
  • Use annotations in the program

发布了50 篇原创文章 · 获赞 33 · 访问量 1万+

Guess you like

Origin blog.csdn.net/qq_25884515/article/details/101202311
Recommended