What is reflection and why is it useful?

前端 未结 21 2816
春和景丽
春和景丽 2020-11-21 04:36

What is reflection, and why is it useful?

I\'m particularly interested in Java, but I assume the principles are the same in any language.

21条回答
  •  梦毁少年i
    2020-11-21 04:46

    As per my understanding:

    Reflection allows programmer to access entities in program dynamically. i.e. while coding an application if programmer is unaware about a class or its methods, he can make use of such class dynamically (at run time) by using reflection.

    It is frequently used in scenarios where a class name changes frequently. If such a situation arises, then it is complicated for the programmer to rewrite the application and change the name of the class again and again.

    Instead, by using reflection, there is need to worry about a possibly changing class name.

提交回复
热议问题