012. private vs protected 问题 - cwy007/tips-and-skills GitHub Wiki

private 与 protected 下 方法的访问权限分别是什么?

The difference

  • Anyone can call your public methods.
  • You can call your protected methods, or another member of your class (or a descendant class) can call your protected methods from the outside. Nobody else can.
  • Only you can call your private methods, because they can only be called with an implicit receiver of self. Even you cannot call self.some_private_method; you must call private_method with self implied. (iGEL points out: "There is one exception, however. If you have a private method age=, you can (and have to) call it with self to separate it from local variables.")

In Ruby, these distinctions are just advice from one programmer to another. Non-public methods are a way of saying "I reserve the right to change this; don't depend on it." But you still get the sharp scissors of send and can call any method you like.

A brief tutorial

# dwarf.rb
class Dwarf
  include Comparable

  def initialize(name, age, beard_strength)
    @name           = name
    @age            = age
    @beard_strength = beard_strength
  end

  attr_reader :name, :age, :beard_strength
  public    :name
  private   :age
  protected :beard_strength

  # Comparable module will use this comparison method for >, <, ==, etc.
  def <=>(other_dwarf)
    # One dwarf is allowed to call this method on another
    beard_strength <=> other_dwarf.beard_strength
  end

  def greet
    "Lo, I am #{name}, and have mined these #{age} years.\
       My beard is #{beard_strength} strong!"
  end

  def blurt
    # Not allowed to do this: private methods can't have an explicit receiver
    "My age is #{self.age}!"
  end
end

require 'irb'; IRB.start

Then you can run ruby dwarf.rb and do this:

gloin = Dwarf.new('Gloin', 253, 7)
gimli = Dwarf.new('Gimli', 62,  9)

gloin > gimli         # false
gimli > gloin         # true

gimli.name            # 'Gimli'
gimli.age             # NoMethodError: private method `age'
                         called for #<Dwarf:0x007ff552140128>

gimli.beard_strength # NoMethodError: protected method `beard_strength'
                        called for #<Dwarf:0x007ff552140128>

gimli.greet          # "Lo, I am Gimli, and have mined these 62 years.\
                           My beard is 9 strong!"

gimli.blurt          # private method `age' called for #<Dwarf:0x007ff552140128>

其他的解释

part 1

  • protected methods can be called by any instance of the defining class or its subclasses.

protected methods 可以被该类的实例和该类的子类的实例调用

  • private methods can be called only from within the calling object. You cannot access another instance's private methods directly.

private methods 只能被当前接收信息的对象(也就是调用方法的对象)调用;这时处于信息中的其他对象,不可以调用private方法(因为这个对象不是当前的信息接受者(the calling object)

@object.method(message) 可以解释为给@object对象传递信息

def compare_to(x)
 self.some_method <=> x.some_method
end

some_method cannot be private here. It must be protected because you need it to support explicit receivers. Your typical internal helper methods can usually be private since they never need to be called like this.

part 2

Private methods in Ruby:

If a method is private in Ruby, then it cannot be called by an explicit receiver (object). It can only be call implicitly. It can be called implicitly by the class in which it has been described in as well as by the subclasses of this class.

ruby 中的private方法不能被显示 explicit 的对象调用,只能被隐式地 implicitly 调用; 可以再该私有private 方法定义的类以及该类的子类中被隐式地调用

  1. A Animal class with private method class_name
class Animal
  def intro_animal
    class_name
  end
  private
  def class_name
    "I am a #{self.class}"
  end
end

In this case:

n = Animal.new
n.intro_animal #=>I am a Animal
n.class_name #=>error: private method `class_name' called
  1. A subclass of Animal called Amphibian:
class Amphibian < Animal
  def intro_amphibian
    class_name
  end 
end 

In this case:

  n= Amphibian.new
  n.intro_amphibian #=>I am a Amphibian
  n.class_name #=>error: private method `class_name' called

As you can see, private methods can be called only implicitly. They cannot be called by explicit receivers. For the same reason, private methods cannot be called outside the hierarchy of the defining class.

正如看到的这样,private methods 只能被隐式地调用。他们不能被明确的接受者(对象)调用。 处于相同的原因,在定义private methods的类及该类的子类之外的地方,这些private methods不能被调用。

part 3

Protected Methods in Ruby:

If a method is protected in Ruby, then it can be called implicitly by both the defining class and its subclasses. Additionally they can also be called by an explicit receiver as long as the receiver is self or of same class as that of self:

  1. A Animal class with protected method protect_me
class Animal
  def animal_call
    protect_me
  end
  protected
  def protect_me
    p "protect_me called from #{self.class}"
  end  
end
  1. A Animal class with protected method protect_me
class Animal
  def animal_call
    protect_me
  end
  protected
  def protect_me
    p "protect_me called from #{self.class}"
  end  
end

In this case:

n= Animal.new
n.animal_call #=> protect_me called from Animal
n.protect_me #=>error: protected method `protect_me' called
  1. A mammal class which is inherited from animal class
class Mammal < Animal
  def mammal_call
    protect_me
  end
end 

In this case

n= Mammal.new
n.mammal_call #=> protect_me called from Mammal
  1. A amphibian class inherited from Animal class (same as mammal class)
class Amphibian < Animal
  def amphi_call
    Mammal.new.protect_me #Receiver same as self
    self.protect_me  #Receiver is self
  end   
end

In this case

n= Amphibian.new
n.amphi_call #=> protect_me called from Mammal
             #=> protect_me called from Amphibian  
  1. A class called Tree
class Tree
  def tree_call
    Mammal.new.protect_me #Receiver is not same as self
  end
end

In this case:

n= Tree.new
n.tree_call #=>error: protected method `protect_me' called for #<Mammal:0x13410c0>

参考连接:https://stackoverflow.com/questions/3534449/why-does-ruby-have-both-private-and-protected-methods