|
|
(4 intermediate revisions by the same user not shown) |
Line 1: |
Line 1: |
|
| |
|
| Ruby's access control has three levels: public, protected and private. The usage of public and protected access levels are the same as that of C++ and Java, but the private level is different. In Ruby, The private methods can only be invoked on the current object of the class. It is incorrect to call on private methods of another object of the same class in the class definition.
| |
| <br>
| |
| for example:<br>
| |
|
| |
| class Person
| |
| private
| |
| def getAge
| |
| return age;
| |
| end
| |
|
| |
| def too_old(other_oject)
| |
| ( self.getAge > 30 ) ? true : false # correct
| |
| ( other_object.getAge > 30 ) ? true : false # ERROR!
| |
| end
| |
| end
| |
|
| |
| In the above code, getAge is defined as a private method. self and other_object are the "receivers" that method. In too_old method, self.getAge is valid expression because the caller of the method is the current object while other_object.getAge is invalide and will cause an error.
| |
|
| |
| However, in Java or C++, it is allowed to call not only the private method of the class itself but also the private method of another object defined by the class.
| |
|
| |
| Think about one situation, the information of a person should be private and no one else can get these information except himself/herself; therefore the method to get person's age should be private.
| |
|
| |
| Take the following Java code for example:
| |
|
| |
| public class Person
| |
| {
| |
| private double age;
| |
|
| |
| private double getAge()
| |
| {
| |
| return this.age;
| |
| }
| |
|
| |
| public boolean isOlder( Person p )
| |
| {
| |
| return ( this.age > p.getAge() ) ? true : false;
| |
| }
| |
| }
| |
|
| |
| In the above code, we assume the member variable age of the class Person is sensitive and no accessor function of this variable should be provided. The method isOlder takes a Person object as a parameter. It compares the age of the current object to the age of the object p passed in. Then isOlder invokes p's private method getAge to make the comparison. This invocation is valid because it is from within the class definition. However, we can simply substitute p.getAge() with p.age since the method is within the class definition.
| |
|
| |
| In object-oriented languages, the private modifer is used to keep the information (i.e. the private member variables) of an object from being accessed by other parties. In our point of view, the only reason that a method tring to call some other object's private method is to obtain the object's relevant information. Otherwise, either the current object can simply invoke its own private method to perform the same functionality or that private method can be as well made public (i.e. there is no sensitive information needs to be protected). If indeed the private method provides some kind of access to the values of private member variables, an accessor function would be a much more elegant and maintainable solution. An accessor has a suggestive method name and is therefore explicit and readable. Even if in some cases, we do not want to provide accessors at all, the methods within the class definition can directly use another object's private member variables if needed, instead of having to invoke its private method.
| |