佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > Ruby 之 class 中的 private、 protected、public

Ruby 之 class 中的 private、 protected、public

熱門標(biāo)簽:Win7旗艦版 呼叫中心市場需求 語音系統(tǒng) 客戶服務(wù) 企業(yè)做大做強 百度AI接口 硅谷的囚徒呼叫中心 電話運營中心
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。
您可能感興趣的文章:
  • Ruby中的public、private、protected區(qū)別小結(jié)
  • 簡單談?wù)凴uby的private和protected

標(biāo)簽:濟南 安康 海南 山西 山西 長沙 喀什 崇左

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Ruby 之 class 中的 private、 protected、public》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    南安市| 庆城县| 宜兰市| 黔南| 耿马| 工布江达县| 天气| 五峰| 临澧县| 黄浦区| 钟祥市| 手机| 高安市| 涡阳县| 西安市| 克什克腾旗| 洪江市| 海淀区| 延长县| 长宁区| 金门县| 沙洋县| 钟祥市| 西乡县| 巴楚县| 左贡县| 通渭县| 民乐县| 香河县| 台南县| 榆中县| 南川市| 贺州市| 天长市| 鄯善县| 星子县| 荔波县| 枣强县| 莱西市| 沙河市| 永顺县|