赞 | 5 |
VIP | 71 |
好人卡 | 22 |
积分 | 6 |
经验 | 32145 |
最后登录 | 2013-8-9 |
在线时间 | 184 小时 |
Lv2.观梦者 天仙
- 梦石
- 0
- 星屑
- 620
- 在线时间
- 184 小时
- 注册时间
- 2008-4-15
- 帖子
- 5023
|
private 就是隐藏用的
例如某些只需要在本类內調用的公用方法(utility method),而不想被外人拿来调用,就可以用private
这些公用方法大概就是一些进行私底下计算用的
Public, Protected, Private Methods:
Instance methods may be public, private, or protected.
*
Public Methods: Methods are normally public unless they are explicitly declared to be private or protected. A public method can be invoked from anywhere, there are no restrictions on its use.
*
Private Methods: A private method is internal to the implementation of a class, and it can only be called by other instance methods of the same class.
*
Protected Methods: 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. It differs from a private method in that it may be explicitly invoked on any instance of the class, and it is not restricted to implicit invocation on self.
These methods can be declared with three methods named public, private, and protected.. Here is the syntax
class Point
# public methods go here
# The following methods are protected
protected
# protected methods go here
# The following methods are private
private
# private methods go here
end
[quote]
Here is a class with a private utility method and a protected accessor method:
[quote]
class Widget
def x # Accessor method for @x
@x
end
protected :x # Make it protected
def utility_method # Define a method
nil
end
private :utility_method # And make it private
end |
|