I need access to some private variables. Can I remove the "private", let them be 开发者_开发技巧public, and the vars still functional well?
If you remove private, they will becobe public, since this is the default access level. But can't you provide methods for dealing with those variables, instead of making them public?
Nothing will "happen", per se. If your code worked with the variables declared private
, then it will also work with them declared as public. Note that the converse is not strictly true, however. If the code was not working because somewhere something was trying to directly access a private
field, making the field public may cause the code to start working.
Longer term, your code may become more difficult to refactor and maintain because by making everything public you've effectively removed any semblance of encapsulation from your design.
yeah they will still work well, but this is not a good programming practice.
Only remove private access modifier if you have a serious cause to do so. Better create a getter for the variable(s).
精彩评论