Summer self-study day 07 | JUnit, reflection, annotation (I)

Time:2021-10-16

JUnit test

  • Use steps
    • Define two annotations @ before and @ after to apply for resources before the test method and release resources after the test method.
    • Define test class: xxxtest()
      • Package name: xxx.test
    • Define test methods in test classes: they can be run independently
      • testMethoda(); testMethodb();
      • Return value: void
      • Parameter list: empty parameter
    • Use the assert operation to confirm whether the results are consistent

reflex

  • Encapsulate the components of a class into other objects (reflection mechanism)
    • Explanation: for example, encapsulate member variables as field objects, construct methods as constructor objects, and member methods as method objects.
    • Benefits:
      • These objects are manipulated while the program is running
      • Decoupling. Improve program scalability.
The stage that Java code goes through in a computer
  • Source code phase

    • The written source file is compiled by javac, and a bytecode file is generated after success
    • The source code enters the class object phase through the class loader
  • Class object phase

    • Class objects go to the next stage by creating objects
  • Runtime phase

    • Create a new object of this class
  • How to get a class object

    • Source code stage: class. Forname (“full class name”)
      • Note: the full class name is the package name and class name
      • Used mostly for configuration files. Read the file and load the class.
    • Class object stage: class name. Class
      • Mostly used for parameter passing
    • Runtime phase: object. Getclass()
      • It is mainly used for obtaining bytecode of objects

        Note: the class objects obtained by these methods are the same object, because a bytecode file is loaded only once when the program runs.
  • Using class objects

    • Some functions:
      • Get member variable, member method, constructor, class name
    • Some common methods:
      • Field[] getFields(), Field getField(String name), Field[] getDeclaredFields(), Field getDeclaredField(StringName)
      • The method for obtaining other data types is similar

This work adoptsCC agreement, reprint must indicate the author and the link to this article

Recommended Today

Swift advanced (XV) extension

The extension in swift is somewhat similar to the category in OC Extension can beenumeration、structural morphology、class、agreementAdd new features□ you can add methods, calculation attributes, subscripts, (convenient) initializers, nested types, protocols, etc What extensions can’t do:□ original functions cannot be overwritten□ you cannot add storage attributes or add attribute observers to existing attributes□ cannot add parent […]