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

后端 未结 7 1802
栀梦
栀梦 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

    Check out "Ruby Programming/Syntax/Classes" for a detailed example and explanation.

    Put simply, the differences between private, public, and protected methods are visibility of that method in the program, kinda like read-only, read and write, and near invisible.

    Unlike some of the other languages, you can't completely hide a Ruby private method, you can only access private methods for your instance of object and not for any other object instance of a class.

    Public, of course, is total accessibility and methods are usually defaulted to public with some exceptions.

    Protected methods are accessible from objects of the same class or even children, which is not the case for a private method.

提交回复
热议问题