引用
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 就不能调用。
分享到:
相关推荐
What's the difference between TAF and FCF
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...
SaaS vs PaaS vs IaaS What's The Difference and How To Choose(英文文档) pdf文件
What are the differences between least-squares and Kalman filtering
Exercise 2.1: What are the differences between int, long, long long, and short? Between an unsigned and a signed type? Between a float and a double? Exercise 2.2: To calculate a mortgage payment, what...
1、Explain the difference between internal and external fragmentation. 2、Given five memory partitions of 100 KB, 500 KB, 200 KB, 300 KB, and 600KB (in order), how would each of the first-fit, best-...
What's the Difference between Pneumatic, Hydraulic, and Electrical Actuators.docx
43. What is the difference between the equilibrium approach and the no-arbitrage approach to modelling? 198 44. How good is the assumption of normal distributions for financial returns? 201 45. How ...
what?is?now?the?open-sourced?NXP?Sensor?Fu- sion?Library.?Stanley?also?wrote?a?series?of?blogs?on?sensor?and?sensor?fusion?topics. As?we?gained?additional?insights?during?the?SenSIP?project,?we?began?...
sva classical q&a Qi1)What is callback ? (Qi2)What is factory pattern ?...(Qi3)Explain the difference between data types logic and reg and wire . (Qi4)What is the need of clocking blocks ?
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 ...
max矩阵含义: What is the difference between GetNodeTM and GetObjectTM? How about LocalTm and WorldTM of a node?
What’s Inside the CloudAn Architectural Map of the Cloud Landscape
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 ...
6.please explain the relationship between classpath and package,and the relationship between them and java files. 7.what is the differense session Bean and Entity Bean ? 8.please write out the meaning...
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 ...
what happens when the program we have written needs to be faster and more efficient, or the application we are using doesn’t do precisely what we want? As computer scientists, we need a basic ...