In object-oriented language, attributes are usually set to private, and then the getter and setter methods are added for access. Some people say that this is essentially different from setting an attribute as public. Simply calculate the attribute as public. Some people have refuted this and destroyed the encapsulation. But what are the disadvantages of destroying encapsulation?
When I was reading a handout on C ++ at Stanford University today, I saw the following statement about this question:
Class involves two roles: implementer and client. The implementer is responsible for implementation and provides interfaces. The client is responsible for calling interfaces, but does not care about implementation. If the attribute becomes public, it is exposed to the client, and the client calls the public attribute. If the implementer modifies the internal implementation of the class, such as deleting a public attribute or changing the name (which is reasonable for the implementer), the client will suffer, code that calls public attributes cannot be compiled. Therefore, the internal architecture should be isolated from the client.
Why set getter and setter?