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

后端 未结 7 1740
栀梦
栀梦 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:40

    public methods are open to everyone. As for private versus protected, I refer to "Ruby Private Methods vs. Protected Methods":

    What is the difference between 'private' and 'protected' methods in Ruby? In Ruby, the primary difference between a 'private' and 'protected' method is that a private method cannot be called with an explicit receiver, while a protected method can. What is an 'explicit receiver', you ask? An explicit receiver is the object that is receiving a message. In the following example, we have a receiver ('parent') and a method ('get_name'). The 'parent' object is receiving the instruction to perform the 'get_name' method.

提交回复
热议问题