Differences between the access control operators public, private, and protected in Ruby, and rubyprivate

Source: Internet
Author: User

Differences between the access control operators public, private, and protected in Ruby, and rubyprivate

Focus on private and protected

Public

The default value is public, which can be accessed globally.

Private

C ++, "private" indicates "private to this class", but Ruby prefers "private to this instance ".
In C ++, As long as class A can access Class A, it can access the private method of the object of Class.
Ruby, but not: You can only access the private method of this object in your object instance.
Because the Ruby principle is "private means you cannot specify the method receiver", the receiver can only be self, and self must be omitted!
Therefore, subclasses in Ruby can access the private method of the parent class. However, self. private_method is incorrect.

Protected

It can be accessed in this class or subclass and cannot be accessed in other classes.

Test code (public access, Code omitted)
class A  def test    protected_mth    private_mth    self.protected_mth    #self.private_mth     #wrong    obj = B.new    obj.protected_mth    #obj.private_mth       #wrong  end  protected  def protected_mth    puts "#{self.class}-protected"  end  private  def private_mth    puts "#{self.class}-private"  endendclass B < A  def test    protected_mth    private_mth    self.protected_mth    #self.private_mth     #wrong    obj = B.new    obj.protected_mth    #obj.private_mth       #wrong  endendclass C  def test    a = A.new    #a.protected_mth          #wrong    #a.private_mth            #wrong  endendA.new.testB.new.testC.new.test

Note: There is no package difference between ruby access control and java.
Classes in other packages only need to reference the target class, which is the same as the class access control rules in the same package.

Reference: http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Classes#Declaring_Visibility


Brief Introduction: different public variables of the public, private, and protected access modifiers are useful for the whole program running.
Private variables only allow direct access within the class
Protected variable class and subclass direct access

Where does the inheritance type mean to put the parent class?
Example:
Class {
Public:
Int a1;
Protected:
Int a2;
Private:
Int a3;
};
A;
A. a1; // OK
A. a2; // error
A. a3; // error
Class B: public {
Public:
Void fb (){
A1; // OK
A2; // OK
A3; // error;
}
};
B B;
B. a1, B. a2, B. a3, and a. a1, a. a2, a. a3. Because class B: public A is equivalent to placing a in the Bpublic part.

Class B: private {};
Then
B. a1, B. a2, and B. a3 both have errors because they are equivalent to placing A in the Bprivate part. From B's perspective, the three changes are both private.
Brief Introduction to private, protected, public, and internal modifier access permission private: private member, which can be accessed only within the class
Protected: protects access to members and inheritance classes.
Public: public members are completely open without access restrictions.
Internal: access within the same namespace

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.