为什么 Ruby 有私有方法和受保护方法?
在我阅读 这篇文章,我认为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 subclassesprivate
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
受保护的
方法可以由定义类或其子类的任何实例调用。私有
方法只能从调用对象内部调用。您无法直接访问另一个实例的私有方法。这是一个快速的实际示例:
这里的
some_method
不能是private
。它必须受到保护
,因为您需要它来支持显式接收器。典型的内部辅助方法通常可以是私有
,因为它们永远不需要像这样被调用。值得注意的是,这与 Java 或 C++ 的工作方式不同。 Ruby 中的
private
与 Java/C++ 中的protected
类似,子类可以访问该方法。在 Ruby 中,无法像 Java 中的private
那样限制子类对方法的访问。无论如何,Ruby 中的可见性很大程度上是一个“推荐”,因为您始终可以使用
send
访问方法: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:
some_method
cannot beprivate
here. It must beprotected
because you need it to support explicit receivers. Your typical internal helper methods can usually beprivate
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 toprotected
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 withprivate
in Java.Visibility in Ruby is largely a "recommendation" anyways since you can always gain access to a method using
send
:区别
self
的隐式接收者来调用。 即使是你也无法调用self.some_private_method
;您必须调用隐含self
的private_method
。self
接收器可以明确地说,允许self.some_private_method
。 (任何其他显式接收器仍然被禁止,即使运行时值与self
相同。)在 Ruby 中,这些区别只是一个程序员给另一个程序员的建议。 非公共方法是一种表达“我保留更改此设置的权利;不要依赖它。”的方式。但是您仍然会得到
send
的锋利剪刀,并且可以调用任何你喜欢的方法。简短教程
然后您可以运行 ruby dwarf.rb 并执行以下操作:
The difference
self
. Even you cannot callself.some_private_method
; you must callprivate_method
withself
implied.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 asself
.)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
Then you can run
ruby dwarf.rb
and do this:Ruby 中的私有方法:
如果一个方法在 Ruby 中是私有的,那么它不能被显式接收者(对象)调用。只能隐式调用。它可以由描述它的类以及该类的子类隐式调用。
下面的示例将更好地说明它:
1) 具有私有方法 class_name 的 Animal 类
在这种情况下:
2) 名为 Amphibian 的 Animal 子类:
在这种情况下:
正如您所看到的,只能隐式调用私有方法。它们不能被显式接收者调用。出于同样的原因,私有方法不能在定义类的层次结构之外调用。
Ruby 中的受保护方法:
如果某个方法在 Ruby 中受保护,那么定义类及其子类都可以隐式调用该方法。此外,只要接收者是 self 或与 self 属于同一类,它们也可以由显式接收者调用:
1) 具有受保护方法 protected_me 的 Animal 类
在这种情况下:
2) 从 Animal 类继承的哺乳动物类
在本例中
3) 两栖类继承自 Animal 类(与哺乳动物类相同)
在本例中
4) 名为 Tree 的类
在本例中:
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
In this case:
2) A subclass of Animal called Amphibian:
In this case:
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
In this case:
2) A mammal class which is inherited from animal class
In this case
3) A amphibian class inherited from Animal class (same as mammal class)
In this case
4) A class called Tree
In this case:
考虑 Java 中的私有方法。当然,它可以从同一个类中调用,但也可以由同一个类的另一个实例调用:
因此,如果调用者是同一类的不同实例,那么我的私有方法实际上可以从可以这么说,“外面”。这实际上使它看起来不那么私密。
另一方面,在 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:
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.
有什么区别?
私有方法解释
我们可以询问 Freddie 事情进展如何,因为它是一个公共方法。这是完全正确的。这很正常并且可以接受。
但是……能够知道弗雷迪内裤情况如何的人,只有弗雷迪本人。对于随机的陌生人来说,把手伸进弗雷迪的内裤是不行的——不,不——这是非常非常私密的事情,我们不想把私密的东西暴露给外界。换句话说,我们可能不想向世界上的任何调用者公开可变数据。有人可能会改变一个值,而我们会陷入一个痛苦的世界,试图找出错误的来源。
受保护方法的解释
考虑一下:
我们可以接受
mrs
获取我们的信用卡详细信息,因为mrs
是我们的骨肉,继承自 Person,所以她可以搞砸它在某些鞋子等上,但我们不希望随机的个人访问我们的信用卡详细信息。如果我们尝试在子类外部执行此操作,则会失败:
摘要
What's the difference?
Private Methods Explained
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.
Protected Methods Explained
Consider this:
We're somewhat ok with
mrs
getting our credit card details, givenmrs
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:
Summary
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.
Java 与 Ruby 的访问控制比较: 如果方法在 Java 中被声明为私有,则它只能被同一类中的其他方法访问。如果某个方法被声明为 protected,则该方法可以被同一包中存在的其他类以及不同包中该类的子类访问。当一个方法是公开的时,它对每个人都是可见的。在 Java 中,访问控制可见性概念取决于这些类在继承/包层次结构中的位置。
而在 Ruby 中,继承层次结构或包/模块不适合。关键在于哪个对象是方法的接收者。
对于 Ruby 中的私有方法,永远不能使用显式接收者来调用它。我们(只能)使用隐式接收器调用私有方法。
这也意味着我们可以从声明它的类以及该类的所有子类中调用私有方法。 >
您永远不能从定义它的类层次结构外部调用私有方法。
受保护的方法可以通过隐式接收器调用,就像私有方法一样。此外,受保护的方法也可以由显式接收者调用(仅当接收者是“自身”或“同一类的对象”时)。
摘要
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.
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".
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.
前三种类型的访问说明符及其定义的范围。
范围。
但我对这个问题有一个解决方案,适用于所有方法如何访问深入解释。
First Three types of access specifiers and those define thier scope.
scope.
But I have a solution for this problem for all method how to access explain in depth.