What is the advantage of having a private attribut

2019-04-06 05:24发布

This question already has an answer here:

In object oriented programming, I used to have this question and I still do :

What would be the benefit of declaring a class member as private if we will create for it a public getter and a public setter?

I don't see any difference at the security level between the case above and the case of declaring the class member as public.

Thanks!

标签: java c++ oop
15条回答
聊天终结者
2楼-- · 2019-04-06 05:59

http://en.wikipedia.org/wiki/Encapsulation_%28object-oriented_programming%29

You could later change the internal representation of the class member, add functionality to the getter and setter (such as notifying an Observer), all without changing the interface (the public getter and setter).

查看更多
ら.Afraid
3楼-- · 2019-04-06 06:01

Encapsulation provides data hiding and more control on the member variables. If an attribute is public then anyone can access it and can assign any value to it. But if your member variable is private and you have provided a setter for it. Then you always have an option to put some constraints check in the setter method to avoid setting an illogical value.

For example a class with public member only :

class MyClass {
    public int age;
}

public MyClassUser {

    public static void main(String args[]) {
        MyClass obj = new MyClass();
        obj.age = -5 // not a logical value for age
    }
}

Same class with private member and a setter:

 class MyClass {
     private int age;
     public void setAge(int age) {
         if(age < 0) {
            // do not use input value and use default
         } else { 
            this.age = age;
         }
     }
 }
查看更多
Explosion°爆炸
4楼-- · 2019-04-06 06:01

I have only one thing to add to the excellent answers that this post have so far.

Sometimes a class attribute could have more than one Getter or Setter, let's illustrate with a silly short example:

class Angle
{
public:
    void Set(MyAngleTypedef a_value) { m_angle = a_value; }
    // Note the 'Radians' type
    void SetRadians(Radians a_value) { m_angle = ConvertRadiansToOurUnit(a_value); }
    // Note the 'Degrees' type
    void SetDegrees(Degrees a_value) { m_angle = ConvertDegreesToOurUnit(a_value); }

    void Get(MyAngleTypedef a_value) const { return m_angle; }
    // Note the 'Radians' type
    Radians GetRadians(Radians a_value) const { return ConvertOurUnitToRadians(m_angle); }
    // Note the 'Degrees' type
    Degrees GetDegrees(Degrees a_value) const { return ConvertOurUnitToDegrees(m_angle); }
private:
    // Raw value of the angle in some user-defined scale.
    MyAngleTypedef m_angle;
}

Is meaningless to store the value more than once for each unit type which you want to work, so the Getters and Setters will provide an interface that makes the class able to work with different units.

IMHO, when a object contains active attributes (attributes that must do some work after or before it is assigned or accessed) it must be a class with only the essential Getters and Setters (the private attributes that doesn't need accessed outside the class, obviously doesn't need public Getters and Setters).

In the other hand if an object conains only passive attributes (attributes that doesn't need extra work when assigned or accessed) it must be a struct and therefore all his attributes would be public accesible without Getters and Setters.

Note that this answer is from the point of view, check this question for more information.

查看更多
贪生不怕死
5楼-- · 2019-04-06 06:07

If the public getter and public setter are just to return the value of the private property and to change its value, then I see no difference.

However, you are implementing encapsulation, so in a later moment you can implement a different behavior, including argument check for setter or write-only/read-only properties, for instance.

查看更多
欢心
6楼-- · 2019-04-06 06:11

I don't see any difference at the security level between the case above and the case of declaring the class member as public.

Immediate question are :

1)What if you want to check some conditions,While setting the value ?

2)What if the subclassess want to return or set something else,by ovveridng that method ?

Other reason:Why getter and setter are better than public fields in Java

查看更多
做个烂人
7楼-- · 2019-04-06 06:12

Beside the encapsulation, consider a situation where your setter is not simply sets a value.
What if you're using it in many classes? And now you realize you want to change the functionality of it? You'll have to change it in whole places where you manually set it. Whereas if you had a setter life would have been easier.

查看更多
登录 后发表回答