What are the advantages of defining a private attribute instead of a public attribute? Why should I have the extra work of creating methods to access and modify privates attributes if I can just make them public?
相关问题
- how to define constructor for Python's new Nam
- Keeping track of variable instances
- What uses more memory in c++? An 2 ints or 2 funct
- Object.create() bug?
- How Does WebSphere Choose the Classloading Order i
相关文章
- 接口B继承接口A,但是又不添加新的方法。这样有什么意义吗?
- NameError: name 'self' is not defined, eve
- Passing static array in attribute
- Implementation Strategies for Object Orientation
- What other neat tricks does the SpecialNameAttribu
- .NET - how to make a class such that only one othe
- Check if the Type of an Object is inherited from a
- When to use Interfaces in PHP
In C# it is idiomatic. That means that public fields would be surprising, and make other people take a little longer to understand your code.
The reason it is idiomatic has to do with the other explanations people are giving, but the fact that it's idiomatic means that you should prefer a property over a public field unless there's a compelling reason to use a field.
Mainly because of OO concept of encapsulation. Using private you encapsulate the access to your object variable keeping the control of the object status. Not allowing external objects to change the status of your object that you are not aware.
A simple example would be a Pocket object.
And now imagine another class as below:
Surely it's a simple example. however this shows how it's important to have control of the access to your class fields/attributes. Imagine a more complex object with a much more complicated state control. Encapsulation makes the abstraction and consistency of the object better.
Typically you would use Private declarations for fields you want to isolate within your program's logic, ONLY if you deem necessary.
Public declarations give you more control of your own program's flow; you can change public fields whenever you wish. You are the man in the house.
So, why most people spontaneously respond with a BIG YES to Private declarations?
Because:
Bottom-line is amazingly "commercial"!! If you are "selling" it, then go private, otherwise do what you like.
A private attribute provides you a level of protection from the users of your class, for that attribute. If you use a public attribute, you will need to add in more logic to test for invalid values up front, which can be more work, as well as more computationally expensive.
Public attributes also "clutter" the interface. The fewer public attributes you present, the "cleaner" and easier your object will be to work with. Having some attributes private gives you flexibility while providing an ease of use that would otherwise not be there.
If you create access method, you separate implementation from interface.
Because you should always strive to defend the implementation from the interface. If you make an attribute public, you are letting the client know how your implementation for that attribute is.
This binds you in keeping not only the interface, but also the implementation.
Also, you can perform other smart things like validation, access control, accounting, if you use a method. If you make an attribute public, you have much less control of what the client can do with that attribute