Does reflection breaks the idea of private methods, because private methods can be access outside of the class?

前端 未结 14 975
甜味超标
甜味超标 2020-11-28 08:06

Does reflection break the idea of private methods? Because private methods can be accessed from outside of the class? (Maybe I don\'t understand the meaning of reflection or

14条回答
  •  有刺的猬
    2020-11-28 08:30

    It's like your house. Locks only keep out honest people, or people who aren't willing to pick your lock.

    Data is data, if someone is determined enough, they can do anything with your code. Literally anything.

    So yes, reflection will allow people to do things you don't want them to do with your code, for example access private fields and methods. However, the important thing is that people will not accidentally do this. If they're using reflection, they know they're doing something they probably aren't intended to do, just like no one accidentally picks the lock on your front door.

提交回复
热议问题