为什么 Ruby 有私有方法和受保护方法?

发布于 2024-09-15 20:11:08 字数 566 浏览 7 评论 0原文

在我阅读 这篇文章,我认为Ruby中的访问控制是这样工作的:

  • public - 可以被任何对象访问(例如Obj.new.public_method
  • protected - 只能从对象本身以及任何子类内部访问
  • private - 与 protected 相同,但该方法不存在于子类中

但是,似乎 < code>protected 和 private 的行为相同,只是您不能使用显式接收者(即 self. protected_method 有效,但 self.private_method 无效)。

这有什么意义呢?什么时候您不希望使用显式接收器调用您的方法?

Before I read this article, I thought access control in Ruby worked like this:

  • public - can be accessed by any object (e.g. Obj.new.public_method)
  • protected - can only be accessed from within the object itself, as well as any subclasses
  • private - same as protected, but the method doesn't exist in subclasses

However, it appears that protected and private act the same, except for the fact that you can't call private methods with an explicit receiver (i.e. self.protected_method works, but self.private_method doesn't).

What's the point of this? When is there a scenario when you wouldn't want your method called with an explicit receiver?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

☆獨立☆ 2024-09-22 20:11:09

受保护的方法可以由定义类或其子类的任何实例调用。

私有方法只能从调用对象内部调用。您无法直接访问另一个实例的私有方法。

这是一个快速的实际示例:

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

这里的 some_method 不能是 private。它必须受到保护,因为您需要它来支持显式接收器。典型的内部辅助方法通常可以是私有,因为它们永远不需要像这样被调用。

值得注意的是,这与 Java 或 C++ 的工作方式不同。 Ruby 中的 private 与 Java/C++ 中的 protected 类似,子类可以访问该方法。在 Ruby 中,无法像 Java 中的 private 那样限制子类对方法的访问。

无论如何,Ruby 中的可见性很大程度上是一个“推荐”,因为您始终可以使用 send 访问方法:

irb(main):001:0> class A
irb(main):002:1>   private
irb(main):003:1>   def not_so_private_method
irb(main):004:2>     puts "Hello World"
irb(main):005:2>   end
irb(main):006:1> end
=> nil

irb(main):007:0> foo = A.new
=> #<A:0x31688f>

irb(main):009:0> foo.send :not_so_private_method
Hello World
=> nil

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

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

Here is a quick practical example:

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.

It is important to note that this is different from the way Java or C++ works. private in Ruby is similar to protected in Java/C++ in that subclasses have access to the method. In Ruby, there is no way to restrict access to a method from its subclasses like you can with private in Java.

Visibility in Ruby is largely a "recommendation" anyways since you can always gain access to a method using send:

irb(main):001:0> class A
irb(main):002:1>   private
irb(main):003:1>   def not_so_private_method
irb(main):004:2>     puts "Hello World"
irb(main):005:2>   end
irb(main):006:1> end
=> nil

irb(main):007:0> foo = A.new
=> #<A:0x31688f>

irb(main):009:0> foo.send :not_so_private_method
Hello World
=> nil
挽手叙旧 2024-09-22 20:11:09

区别

  • 任何人都可以调用您的公共方法。
  • 您可以调用您的受保护方法,或者您的类(或后代类)的另一个成员可以从外部调用您的受保护方法。其他人都做不到。
  • 只有您可以调用您的私有方法,因为它们只能通过 self 的隐式接收者来调用。 即使是你也无法调用self.some_private_method;您必须调用隐含 selfprivate_method
    • iGEL指出:“但是有一个例外。如果你有一个私有方法age=,你可以(并且必须)用 self 调用它以将其与局部变量分开。”
    • Ruby 2.7 起,self 接收器可以明确地说,允许 self.some_private_method 。 (任何其他显式接收器仍然被禁止,即使运行时值与 self 相同。)

在 Ruby 中,这些区别只是一个程序员给另一个程序员的建议。 非公共方法是一种表达“我保留更改此设置的权利;不要依赖它。”的方式。但是您仍然会得到 send 的锋利剪刀,并且可以调用任何你喜欢的方法。

简短教程

# 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

然后您可以运行 ruby​​ dwarf.rb 并执行以下操作:

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>

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."
    • Since Ruby 2.7 the self receiver can be explicit, self.some_private_method is allowed. (Any other explicit receiver is still disallowed, even if the runtime value is the same as self.)

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>
你与昨日 2024-09-22 20:11:09

Ruby 中的私有方法:

如果一个方法在 Ruby 中是私有的,那么它不能被显式接收者(对象)调用。只能隐式调用。它可以由描述它的类以及该类的子类隐式调用。

下面的示例将更好地说明它:

1) 具有私有方法 class_name 的 Animal 类

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

在这种情况下:

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

2) 名为 Amphibian 的 Animal 子类:

class Amphibian < Animal
  def intro_amphibian
    class_name
  end 
end 

在这种情况下:

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

正如您所看到的,只能隐式调用私有方法。它们不能被显式接收者调用。出于同样的原因,私有方法不能在定义类的层次结构之外调用。

Ruby 中的受保护方法:

如果某个方法在 Ruby 中受保护,那么定义类及其子类都可以隐式调用该方法。此外,只要接收者是 self 或与 self 属于同一类,它们也可以由显式接收者调用:

1) 具有受保护方法 protected_me 的 Animal 类

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

在这种情况下:

n= Animal.new
n.animal_call #=> protect_me called from Animal
n.protect_me #=>error: protected method `protect_me' called

2) 从 Animal 类继承的哺乳动物类

class Mammal < Animal
  def mammal_call
    protect_me
  end
end 

在本例中

n= Mammal.new
n.mammal_call #=> protect_me called from Mammal

3) 两栖类继承自 Animal 类(与哺乳动物类相同)

class Amphibian < Animal
  def amphi_call
    Mammal.new.protect_me #Receiver same as self
    self.protect_me  #Receiver is self
  end   
end

在本例中

n= Amphibian.new
n.amphi_call #=> protect_me called from Mammal
             #=> protect_me called from Amphibian  

4) 名为 Tree 的类

class Tree
  def tree_call
    Mammal.new.protect_me #Receiver is not same as self
  end
end

在本例中:

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

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.

The following examples will illustrate it better:

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

2) 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.

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

In this case:

n= Animal.new
n.animal_call #=> protect_me called from Animal
n.protect_me #=>error: protected method `protect_me' called

2) 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

3) 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  

4) 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>
琉璃繁缕 2024-09-22 20:11:09

考虑 Java 中的私有方法。当然,它可以从同一个类中调用,但也可以由同一个类的另一个实例调用:

public class Foo {

   private void myPrivateMethod() {
     //stuff
   }

   private void anotherMethod() {
       myPrivateMethod(); //calls on self, no explicit receiver
       Foo foo = new Foo();
       foo.myPrivateMethod(); //this works
   }
}

因此,如果调用者是同一类的不同实例,那么我的私有方法实际上可以从可以这么说,“外面”。这实际上使它看起来不那么私密。

另一方面,在 Ruby 中,私有方法实际上只对当前实例是私有的。这就是删除显式接收者的选项所提供的。

另一方面,我当然应该指出,在 Ruby 社区中,根本不使用这些可见性控件是很常见的,因为 Ruby 无论如何都为您提供了绕过它们的方法。与 Java 世界不同,趋势是让一切都变得可访问,并相信其他开发人员不会把事情搞砸。

Consider a private method in Java. It can be called from within the same class, of course, but it can also be called by another instance of that same class:

public class Foo {

   private void myPrivateMethod() {
     //stuff
   }

   private void anotherMethod() {
       myPrivateMethod(); //calls on self, no explicit receiver
       Foo foo = new Foo();
       foo.myPrivateMethod(); //this works
   }
}

So -- if the caller is a different instance of my same class -- my private method is actually accessible from the "outside", so to speak. This actually makes it seem not all that private.

In Ruby, on the other hand, a private method really is meant to be private only to the current instance. This is what removing the option of an explicit receiver provides.

On the other hand, I should certainly point out that it's pretty common in the Ruby community to not use these visibility controls at all, given that Ruby gives you ways to get around them anyway. Unlike in the Java world, the tendency is to make everything accessible and trust other developers not to screw things up.

吝吻 2024-09-22 20:11:09

有什么区别?

  • 故事旨在帮助您记住。

私有方法解释

@freddie = Person.new
@freddie.hows_it_going?
# => "oh dear, i'm in great pain!"

class Person   
    # public method
    def hows_it_going?
        how_are_your_underpants_feeling?
    end

    private

    def how_are_your_underpants_feeling? # private method
        puts "oh dear, i'm in great pain!"
    end
end

我们可以询问 Freddie 事情进展如何,因为它是一个公共方法。这是完全正确的。这很正常并且可以接受。

但是……能够知道弗雷迪内裤情况如何的人,只有弗雷迪本人。对于随机的陌生人来说,把手伸进弗雷迪的内裤是不行的——不,不——这是非常非常私密的事情,我们不想把私密的东西暴露给外界。换句话说,我们可能不想向世界上的任何调用者公开可变数据。有人可能会改变一个值,而我们会陷入一个痛苦的世界,试图找出错误的来源。

@freddie.how_are_your_underpants_feeling?
# => # NoMethodError: private method `how_are_your_underpants_feeling?' called

受保护方法的解释

考虑一下:

class Person    
    protected

    def hand_over_the_credit_card! # protected method
        puts "Lawd have mercy. Whatever. Here it is: 1234-4567-8910"
    end
end

class Rib < Person
end

class Wife < Rib # wife inherits from Rib
    def i_am_buying_another_handbag_with_your_card(husband)        
        husband.hand_over_the_credit_card! # equalityInAction    
    end
end

@husband = Person.new
@mrs = Wife.new
@mrs.i_am_buying_another_handbag_with_your_card(@husband)
# => puts "Lawd have mercy. Whatever. Here it is: 1234-4567-8910"

我们可以接受 mrs 获取我们的信用卡详细信息,因为 mrs 是我们的骨肉,继承自 Person,所以她可以搞砸它在某些鞋子等上,但我们不希望随机的个人访问我们的信用卡详细信息。

如果我们尝试在子类外部执行此操作,则会失败:

@mrs = Wife.new
@mrs.hand_over_the_credit_card!
# => protected method hand_over_the_credit_card! called for #<Wife:0x00005567b5865818> (NoMethodError)

摘要

  • 私有方法只能从内部调用,并且没有“显式接收者”。 (严格来说,您可以使用一点 ruby​​ 魔法来访问私有方法,但我暂时忽略它)。
  • 受保护的方法可以在子类中调用。
  • 我用例子/类比来帮助你生动地记住。

What's the difference?

  • Story given to help you remember.

Private Methods Explained

@freddie = Person.new
@freddie.hows_it_going?
# => "oh dear, i'm in great pain!"

class Person   
    # public method
    def hows_it_going?
        how_are_your_underpants_feeling?
    end

    private

    def how_are_your_underpants_feeling? # private method
        puts "oh dear, i'm in great pain!"
    end
end

We can ask Freddie how things are going given it's a public method. That's perfectly valid. And it's normal and accepted.

But...the only person who can know how Freddie's underpants situation is, is Freddie himself. It would not do for random strangers to reach into Freddy's underpants - no, no -- it's very, very private, and we don't want to expose what's private to the outside world. In other words, we may not want to expose mutable data, to any caller in the world. Someone could mutate a value, and we'd be in a world of pain trying to discover where the bug came from.

@freddie.how_are_your_underpants_feeling?
# => # NoMethodError: private method `how_are_your_underpants_feeling?' called

Protected Methods Explained

Consider this:

class Person    
    protected

    def hand_over_the_credit_card! # protected method
        puts "Lawd have mercy. Whatever. Here it is: 1234-4567-8910"
    end
end

class Rib < Person
end

class Wife < Rib # wife inherits from Rib
    def i_am_buying_another_handbag_with_your_card(husband)        
        husband.hand_over_the_credit_card! # equalityInAction    
    end
end

@husband = Person.new
@mrs = Wife.new
@mrs.i_am_buying_another_handbag_with_your_card(@husband)
# => puts "Lawd have mercy. Whatever. Here it is: 1234-4567-8910"

We're somewhat ok with mrs getting our credit card details, given mrs is flesh of our flesh, inherited from Person, so she can blow it on some shoes etc., but we don't want random individuals getting access to our credit card details.

If we tried to do that outside the subclass, it would fail:

@mrs = Wife.new
@mrs.hand_over_the_credit_card!
# => protected method hand_over_the_credit_card! called for #<Wife:0x00005567b5865818> (NoMethodError)

Summary

  • private methods can only be called from within, and without "an explicit receiver". (Strictly speaking, you can access a private method using a little bit of ruby magic, but I'm going to ignore that for the moment).
  • protected methods can be called within sub-classes.
  • I used the examples/analogies to help you vividly remember.
惯饮孤独 2024-09-22 20:11:09

Ruby 中的子类可以访问私有方法的部分原因是 Ruby 对类的继承是对 Module Includes 的薄薄的糖衣——在 Ruby 中,类实际上是一种提供继承等的模块。

http://ruby-doc.org/core-2.0.0/Class.html

这意味着子类基本上“包含”父类,以便有效地在子类中定义父类的函数,包括私有函数

在其他编程语言中,调用方法涉及将方法名称沿父类层次结构向上冒泡,并查找响应该方法的第一个父类。相比之下,在 Ruby 中,虽然父类层次结构仍然存在,但父类的方法直接包含在子类已定义的方法列表中。

Part of the reason why private methods can be accessed by subclasses in Ruby is that Ruby inheritance with classes is thin sugarcoating over Module includes - in Ruby, a class, in fact, is a kind of module that provides inheritance, etc.

http://ruby-doc.org/core-2.0.0/Class.html

What this means is that basically a subclass "includes" the parent class so that effectively the parent class's functions, including private functions, are defined in the subclass as well.

In other programming languages, calling a method involves bubbling the method name up a parent class hierarchy and finding the first parent class that responds to the method. By contrast, in Ruby, while the parent class hierarchy is still there, the parent class's methods are directly included into the list of methods of the subclass has defined.

大姐,你呐 2024-09-22 20:11:09

Java 与 Ruby 的访问控制比较: 如果方法在 Java 中被声明为私有,则它只能被同一类中的其他方法访问。如果某个方法被声明为 protected,则该方法可以被同一包中存在的其他类以及不同包中该类的子类访问。当一个方法是公开的时,它对每个人都是可见的。在 Java 中,访问控制可见性概念取决于这些类在继承/包层次结构中的位置。

而在 Ruby 中,继承层次结构或包/模块不适合。关键在于哪个对象是方法的接收者。

对于 Ruby 中的私有方法,永远不能使用显式接收者来调用它。我们(只能)使用隐式接收器调用私有方法。

这也意味着我们可以从声明它的类以及该类的所有子类中调用私有方法。 >

class Test1
  def main_method
    method_private
  end

  private
  def method_private
    puts "Inside methodPrivate for #{self.class}"
  end
end

class Test2 < Test1
  def main_method
    method_private
  end
end

Test1.new.main_method
Test2.new.main_method

Inside methodPrivate for Test1
Inside methodPrivate for Test2

class Test3 < Test1
  def main_method
    self.method_private #We were trying to call a private method with an explicit receiver and if called in the same class with self would fail.
  end
end

Test1.new.main_method
This will throw NoMethodError

您永远不能从定义它的类层次结构外部调用私有方法。

受保护的方法可以通过隐式接收器调用,就像私有方法一样。此外,受保护的方法也可以由显式接收者调用(仅当接收者是“自身”或“同一类的对象”时)。

 class Test1
  def main_method
    method_protected
  end

  protected
  def method_protected
    puts "InSide method_protected for #{self.class}"
  end
end

class Test2 < Test1
  def main_method
    method_protected # called by implicit receiver
  end
end

class Test3 < Test1
  def main_method
    self.method_protected # called by explicit receiver "an object of the same class"
  end
end


InSide method_protected for Test1
InSide method_protected for Test2
InSide method_protected for Test3


class Test4 < Test1
  def main_method
    Test2.new.method_protected # "Test2.new is the same type of object as self"
  end
end

Test4.new.main_method

class Test5
  def main_method
    Test2.new.method_protected
  end
end

Test5.new.main_method
This would fail as object Test5 is not subclass of Test1
Consider Public methods with maximum visibility

摘要

Public:公共方法具有最大可见性

Protected:受保护的方法可以是使用隐式接收者调用,就像 private 一样。此外,受保护的方法也可以由显式接收者调用(仅)如果接收者是“自身”或“同一类的对象”。

Private:对于 Ruby 中的私有方法,永远不能使用显式接收者来调用它。我们(只能)使用隐式接收器调用私有方法。这也意味着我们可以从声明它的类以及该类的所有子类中调用私有方法。

Comparison of access controls of Java against Ruby: If method is declared private in Java, it can only be accessed by other methods within the same class. If a method is declared protected it can be accessed by other classes which exist within the same package as well as by subclasses of the class in a different package. When a method is public it is visible to everyone. In Java, access control visibility concept depends on where these classes lie's in the inheritance/package hierarchy.

Whereas in Ruby, the inheritance hierarchy or the package/module don't fit. It's all about which object is the receiver of a method.

For a private method in Ruby, it can never be called with an explicit receiver. We can (only) call the private method with an implicit receiver.

This also means we can call a private method from within a class it is declared in as well as all subclasses of this class.

class Test1
  def main_method
    method_private
  end

  private
  def method_private
    puts "Inside methodPrivate for #{self.class}"
  end
end

class Test2 < Test1
  def main_method
    method_private
  end
end

Test1.new.main_method
Test2.new.main_method

Inside methodPrivate for Test1
Inside methodPrivate for Test2

class Test3 < Test1
  def main_method
    self.method_private #We were trying to call a private method with an explicit receiver and if called in the same class with self would fail.
  end
end

Test1.new.main_method
This will throw NoMethodError

You can never call the private method from outside the class hierarchy where it was defined.

Protected method can be called with an implicit receiver, as like private. In addition protected method can also be called by an explicit receiver (only) if the receiver is "self" or "an object of the same class".

 class Test1
  def main_method
    method_protected
  end

  protected
  def method_protected
    puts "InSide method_protected for #{self.class}"
  end
end

class Test2 < Test1
  def main_method
    method_protected # called by implicit receiver
  end
end

class Test3 < Test1
  def main_method
    self.method_protected # called by explicit receiver "an object of the same class"
  end
end


InSide method_protected for Test1
InSide method_protected for Test2
InSide method_protected for Test3


class Test4 < Test1
  def main_method
    Test2.new.method_protected # "Test2.new is the same type of object as self"
  end
end

Test4.new.main_method

class Test5
  def main_method
    Test2.new.method_protected
  end
end

Test5.new.main_method
This would fail as object Test5 is not subclass of Test1
Consider Public methods with maximum visibility

Summary

Public: Public methods have maximum visibility

Protected: Protected method can be called with an implicit receiver, as like private. In addition protected method can also be called by an explicit receiver (only) if the receiver is "self" or "an object of the same class".

Private: For a private method in Ruby, it can never be called with an explicit receiver. We can (only) call the private method with an implicit receiver. This also means we can call a private method from within a class it is declared in as well as all subclasses of this class.

半窗疏影 2024-09-22 20:11:09

前三种类型的访问说明符及其定义的范围。

  1. 公共->访问课堂外的任何地方。
  2. 私人 ->课外无法访问。
  3. 受保护->该方法无法访问该方法定义的任何地方
    范围。

但我对这个问题有一个解决方案,适用于所有方法如何访问深入解释。

class Test
  attr_reader :name

  def initialize(name)
    @name = name
  end
  
  def add_two(number)
    @number = number 
  end
  
  def view_address
    address("Anyaddress")
  end
  
  private 

  def address(add)
    @add = add
  end
  
  protected 

  def user_name(name)
    # p 'call method'
    @name = name
  end
end

class Result < Test
  def new_user
    user_name("test355")
  end
end
  1. 对象列表
  2. p test = Test.new("test")
  3. p test.name
  4. p test.add_two(3)
  5. 列表项
  6. p test.view_address
  7. p r = Result.new("")
  8. p r.new_user

First Three types of access specifiers and those define thier scope.

  1. Public -> Access anywhere out side the class.
  2. Private -> Can not access outside the class.
  3. Protected -> This Method not access anywhere this method define
    scope.

But I have a solution for this problem for all method how to access explain in depth.

class Test
  attr_reader :name

  def initialize(name)
    @name = name
  end
  
  def add_two(number)
    @number = number 
  end
  
  def view_address
    address("Anyaddress")
  end
  
  private 

  def address(add)
    @add = add
  end
  
  protected 

  def user_name(name)
    # p 'call method'
    @name = name
  end
end

class Result < Test
  def new_user
    user_name("test355")
  end
end
  1. Object List
  2. p test = Test.new("test")
  3. p test.name
  4. p test.add_two(3)
  5. List item
  6. p test.view_address
  7. p r = Result.new("")
  8. p r.new_user
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文