ruby系列教材(18):Specifying Access Control

来源:百度文库 编辑:神马文学网 时间:2024/07/08 10:53:26
本篇的内容主要描述Access Control的实现:
1)方式一
class MyClass
def method1                                               # default is ‘public‘
#...
end
protected                                                   # subsequent methods will be ‘protected‘
def method2                                              # will be ‘protected‘
#...
end
private                                                       # subsequent methods will be ‘private‘
def method3                                              # will be ‘private‘
#...
end
public                                                         # subsequent methods will be ‘public‘
def method4                                              # and this will be ‘public‘
#...
end
end
2)方式二
class MyClass
def method1
end
# ... and so on
public            :method1, :method4
protected      :method2
private          :method3
end
*在public 等后面加上Symbol literals