What does the “private” modifier do?

前端 未结 13 976
予麋鹿
予麋鹿 2021-01-17 07:24

Considering \"private\" is the default access modifier for class Members, why is the keyword even needed?

13条回答
  •  悲哀的现实
    2021-01-17 07:59

    The private modifier explains intent.

    A private member variable is not intended for direct manipulation outside the class. get/set accessors may or may not be created for the variable.

    A private method is not intended for use outside the class. This may be for internal functionality only. Or you could make a default constructor private to prevent the construction of the class without passing in values.

    The private modifier (and others like it) can be a useful way of writing self documenting code.

提交回复
热议问题