JavaWeb学习总结:反射

  • Post author:
  • Post category:java




反射

概念:将类的各个组成部分封装为其他对象,这就是反射机制

Class对象功能:

  • 获取功能:

    • 1,获取成员变量

      • Field[ ] getFields():获取所有public修饰的

      • Field getField(String name):获取指定名称的public修饰的成员变量

      • Field[ ] getDeclaredFields() :获取所有的成员变量,不考虑修饰符

      • Field getDeclaredField(String name)

    • 2,获取构造方法

      • Constructor<?>[ ] getConstructors()

      • Constructor getConstructor(类<?>…parameterTypes)

      • Constructor getDeclaredConstructor(类<?>…parameterTypes)

      • Constructor<?>[ ] getDeclaredConstructors()

    3,获取成员方法

    * Method[ ] getMethods()

    * Method getMethod(String name,类<?>…parameterTypes)

      * Method[ ] getDeclaredMethods()
      * Method getDeclaredMethod(String name,类<?>...parameterTypes)
    

    4,获取类名

    * string getName()

  • Field:成员变量

    • 操作:

      • 1,设置值

        • void set(Object obj,Object value)
      • 2,获取值

        • get(Object obj)
      • 3,忽略访问权限修饰符的安全检查

        • setAccessible(true):暴力反射



版权声明:本文为weixin_42384085原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。