What is reflection and why is it useful?

前端 未结 21 3451
春和景丽
春和景丽 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条回答
  •  不要未来只要你来
    2020-11-21 05:07

    Example:

    Take for example a remote application which gives your application an object which you obtain using their API Methods . Now based on the object you might need to perform some sort of computation .

    The provider guarantees that object can be of 3 types and we need to perform computation based on what type of object .

    So we might implement in 3 classes each containing a different logic .Obviously the object information is available in runtime so you cannot statically code to perform computation hence reflection is used to instantiate the object of the class that you require to perform the computation based on the object received from the provider .

提交回复
热议问题