简体   繁体   English

受Ruby保护

[英]Protected in Ruby

I have this question: 我有这个问题:

Class members in Ruby marked as protected Ruby中的类成员标记为受保护

a. 一种。 Can only be accessed by the exact instance that owns those members 只能由拥有这些成员的确切实例访问
b. b。 (a), and also any other instances of that class (a),以及该类别的其他任何实例
c. C。 (a), (b), and also any subclasses of that class (a),(b)以及该类别的任何子类别
d. d。 (a), and any module code imported into that class (a),以及导入该类的任何模块代码

The answer I'm told is b. 我被告知的答案是b。 But I think it should be c, as a subclass is also an instance of the super class (according to our notes) I was hoping someone with some more Ruby knowledge could help me out. 但是我认为应该是c,因为子类也是超类的一个实例(根据我们的笔记),我希望有人对Ruby有更多的了解可以帮助我。

"A protected method is like a private method in that it can only be invoked from within the implementation of a class or its subclasses. (...)" (The Ruby Programming Language, Flanagan & Matsumoto, page 232) “一个受保护的方法就像一个私有方法,因为它只能在一个类或其子类的实现中调用。(...)”(Ruby编程语言,Flanagan和Matsumoto,第232页)

So yes, answer c seems correct. 因此,答案c似乎正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM