What does the “private” modifier do?

2020-04-01 08:42发布

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

13条回答
你好瞎i
2楼-- · 2020-04-01 09:15

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.

查看更多
登录 后发表回答