引用
In Ruby, private visibility is what protected was in Java. However, you can't have
truly private methods in Ruby; you can't completely hide a method.
《The Ruby Way》中第21页 写道
private意味着方法只可被当前类或其子类使用,只能通过“函数形式”调用——隐式或显式地将self作为接收方。protected意味着方法只能在当前类中调用,但与private不同,调用时可将非self指定为接收方。
protected主要是用于这种情况:想让某个类的一个实例与该类的另一个实例协作完成某些任务。
class O
def initialize(name)
@name = name
end
def ==(o)
@name == o.name
end
protected
def name
@name
end
end
o1 = O.new('name1')
o1.name # NoMethodError: protected method called
o2 = O.new('name2')
o1 == o2 # false
这时候,如果这个类的 name 不想让外部访问,就必须设置成 protected
如果是 private 的话,== 方法中的 o.name 就不能调用。
分享到:
相关推荐
1-1 In UMTS,what’s the difference between AAL2 and AAL5? 1-2 In UMTS,what do FDD and TDD mean? 1-3 Describe the processor of hardover and soft handover? 1-4 Describe the spread mechanism used in UMTS...
Public methods (getters and setters) are then provided to manipulate these private members, ensuring data integrity and controlling the interaction with the object's internal state. 9. What are the ...
Unit2 What's the matter, Mike测试题及答案A卷2.doc
3) What is the difference between split and nosplit scheme? In general, splitting the image into four sets and looping on them is computational faster. However, in some cases like deconvolution, we ...
Hampson, S....What is the difference between personality and social psychology? For a school psychologist, this is probably not a particularly important question. However, it is somewh
本书详细介绍了数学的各个分支。对于有需要了解数学方法的程序员有很大的帮助。
What are the tradeoffs between normalized and denormalized data? How do you handle replica set failure and failover? This collection of MongoDB tips, tricks, and hacks helps you resolve issues with ...
`: 这是一种询问差异的直接方式,例如:“What’s the difference between a laptop and a desktop computer?” 询问笔记本电脑和台式电脑的不同之处。 4. `There be some differences between A and B`: 这句话...
Hampson, S....What is the difference between personality and social psychology? For a school psychologist, this is probably not a particularly important question. However, it is somewh
What is the difference between a task and a process? Do I need to worry about reentrancy? As we progress through Embedded Systems Firmware Demystified, you will come to see that these questions are ...
1. What is the IP address and TCP port number used by the client computer (source) that is transferring the file to gaia.cs.umass.edu? To answer this question, it’s probably easiest to select an ...
What is the major difference between Oracle 8i and Oracle 9i? **Answer**: The differences between Oracle 8i and Oracle 9i are detailed on the official Oracle website. However, some key distinctions ...
和...有什么区别 ...示例:“人与动物” 3-按Enter 4-它将在Google上搜索为“人与动物之间有什么区别” Github https://github.com/volkanakinpasa/what-is-the-different-between 支持语言:English
Q11) What is the difference between SEARCH and SEARCH ALL? A11) SEARCH - is a serial search.SEARCH ALL - is a binary search & the table must be sorted ( ASCENDING/DESCENDING KEY clause to be used & ...
A computer's architecture is its abstract model and is the programmer's view in terms of instructions, addressing modes and registers. A computer's organization expresses the realization of the ...
练习题旨在帮助学生巩固“Unit 5 What's the weather like today”这一单元的学习成果,涉及了听、说、读、写等多个语言技能。 首先,我们来看一下题目中的关键单词及其英文翻译: 1. 说 - say 2. 将,愿意 - will ...
Open Source Intelligence Methods and Tools focuses on building a deep understanding of how to exploit open source intelligence (OSINT) techniques, methods, and tools to acquire information from ...
37. What is the difference between the SAP HANA enterprise edition and the SAP HANA platform edition? 38. Are there any special SAP HANA options that can be purchased on top of the standard SAP HANA ...