最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Ruby 之 class 中的 private、 protected、public

 更新時間:2009年11月26日 19:09:47   作者:  
Ruby 之 class 中的 private、 protected、public
Private
private 函數(shù)只能 在本類和子類的 上下文中調(diào)用,且只能通過self訪問。

這個意思就是:private函數(shù),只能在本對象內(nèi)部訪問到。

對象實例變量(@)的訪問權(quán)限就是 private。
復(fù)制代碼 代碼如下:

class AccessTest
def test
return “test private”
end
def test_other(other)
“other object ”+ other.test
end
end
t1 = AccessTest.new
t2 = AccessTest.new

p t1.test # => test private

p t1.test_other(t2) # => other object test private


# Now make 'test' private

class AccessTest
private :test
end

p t1.test_other(t2) #錯誤 in `test_other': private method `test' called for #<AccessTest:0x292c14> (NoMethodError)


Protected
protect 函數(shù)只能 在本類和子類的 上下文中調(diào)用,但可以使用 other_object.function的形式。(這跟 C++ 的 private 模式等同)

這個的關(guān)鍵是 protected函數(shù)可以在同類(含子類)的其它對象的內(nèi)部中使用。

# Now make 'test' protect

class AccessTest
protected:test
end

p t1.test_other(t2) # other object test private

Public
public 函數(shù)可以在任何地方調(diào)用。成員函數(shù)和常量的默認訪問權(quán)限就是public。

相關(guān)文章

最新評論

拉萨市| 乌拉特后旗| 满洲里市| 北流市| 丰顺县| 安新县| 丰城市| 思南县| 陆川县| 大新县| 当阳市| 日照市| 钦州市| 石城县| 华容县| 海阳市| 侯马市| 舟曲县| 金塔县| 昌平区| 纳雍县| 兰溪市| 玛沁县| 克拉玛依市| 卓资县| 仁怀市| 齐河县| 抚顺县| 永福县| 巴马| 乌鲁木齐市| 陕西省| 沅江市| 墨玉县| 绵竹市| 阿拉善左旗| 澳门| 贺兰县| 南部县| 临泽县| 红原县|