Class attribute declaration: private vs public

2019-04-19 13:04发布

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?

9条回答
forever°为你锁心
2楼-- · 2019-04-19 13:11

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.

查看更多
The star\"
3楼-- · 2019-04-19 13:15

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.

    class Pocket {
 public int numberOfCoins = 10;
 private boolean haveMoney = true;

 public void giveOneCoin(){
    if(stillHaveMoney()){
     numberOfCoins--;
     if(numberOfCoins=<0){
         haveMoney=false;
     }
   }
 }

 public boolean stillHaveMoney(){
  return haveMoney;
 }

}

And now imagine another class as below:

class PickPockets {
   public void getSomeMoney(Pocket pocket){
      pocket.numberOfCoins=0;
   }
}

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.

查看更多
女痞
4楼-- · 2019-04-19 13:19

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:

  • They were taught to do it "just like that" in college. (most prudent reason)
  • They ultimately, foolishly and immediately assume that you are either building a code library, building a software component for others to consume OR working on some cryptography implementation. ONLY in these cases would you not be sure what would happen to your fields.

Bottom-line is amazingly "commercial"!! If you are "selling" it, then go private, otherwise do what you like.

查看更多
淡お忘
5楼-- · 2019-04-19 13:23

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.

查看更多
Animai°情兽
6楼-- · 2019-04-19 13:26

If you create access method, you separate implementation from interface.

查看更多
你好瞎i
7楼-- · 2019-04-19 13:27

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

查看更多
登录 后发表回答