What are the differences between “private”, “public”, and “protected methods”?

后端 未结 7 1790
栀梦
栀梦 2020-12-08 04:03

I\'m learning Ruby, and have come up to a point where I am confused.

The book I am using is talking about private, public, and protec

7条回答
  •  半阙折子戏
    2020-12-08 04:57

    The difference will be on Visibility and how they are affected by Inheritance :

    Visibility :

    || Anywhere || Public can be accessed from inside and outside the class.

    || Inside the class || Both Private and Protected can only be accessed from inside the class.

    The similarity between Protected and Private :

    • Both can be accessed from outside the class through a public method.

    The differences between Protected and Private are :

    • Private method can not be called with a receiver (not even with #self). UNLESS ... calling a PRIVATE SETTER method. If you try to remove the receiver, Ruby will create a local variable. Self is a must in this case.

    • Protected may or may not use self.

    • Protected can access another object's protected method that comes from the same class, Private can't.

    When it comes to Inheritance :

    • Private methods can only be called on subclasses implicitly (simply just the name of the method) but not explicitly (using #self).

    • Protected can be called both ways (with or without #self || implicitly or explicitly).

    Example with code below :

     class Dog
      attr_accessor :name, :age
    
      def initialize(n, a)
        self.name = n
        self.age = a
      end
    
      def accessing_private
        "#{self.name} in human years is #{human_years}. This is secret!"
      end
    
      def accessing_protected
        "Will this work? " + a_protected_method
      end
    
      def eat_more_than(other) 
      # accessing other instance's protected method from the same class
        daily_diet < other.daily_diet 
        "#{name} eats more than #{other.name}"
      end
    
      def boy 
        gender_method("boy") # accessing private setter method
      end
    
      protected
    
      def daily_diet 
        age * 2 # the younger, the more they have to eat 
      end
    
      def a_protected_method
        "Yes, I'm protected!"
      end
    
      private
    
      attr_writer :gender
    
      def gender_method(gender)
        self.gender = gender # private setter method requires self
        "#{name} is a #{gender}"
      end
    
      def human_years
        age * 8
      end
    end
    
    # Create the first object of Dog
    blake = Dog.new("Blake", 5)
    
    p blake.accessing_private # "Blake in human years is 16. This is secret!"
    
    p blake.accessing_protected # "Will this work? Yes, I'm protected!"
    
    # Create the second object of Dog
    jackson = Dog.new("Jackson", 1)
    
    # Below, protected methods from different objects of the same type/class 
    # are proven to share access
    p jackson.eat_more_than(blake) # true -> "Jackson eats more than Blake"
    
    # Below, accessing private setter method through a public method.
    p blake.boy # Blake is a boy 
    

提交回复
热议问题