濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > Ruby 之 class 中的 private、 protected、public

Ruby 之 class 中的 private、 protected、public

熱門標(biāo)簽:外呼線路從哪里出來(lái)的 天津外呼系統(tǒng)怎么收費(fèi) 柯城手機(jī)地圖如何做地圖標(biāo)注 AI電銷機(jī)器人 線路 巫師3地圖標(biāo)注魔力之所 征服者企業(yè)地圖標(biāo)注 漯河電銷 中牟外呼系統(tǒng)違法嗎 淮安自動(dòng)外呼系統(tǒng)供應(yīng)商
Private
private 函數(shù)只能 在本類和子類的 上下文中調(diào)用,且只能通過(guò)self訪問(wèn)。

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

對(duì)象實(shí)例變量(@)的訪問(wèn)權(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) #錯(cuò)誤 in `test_other': private method `test' called for #AccessTest:0x292c14> (NoMethodError)


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

這個(gè)的關(guān)鍵是 protected函數(shù)可以在同類(含子類)的其它對(duì)象的內(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ù)和常量的默認(rèn)訪問(wèn)權(quán)限就是public。
您可能感興趣的文章:
  • Ruby中的public、private、protected區(qū)別小結(jié)
  • 簡(jiǎn)單談?wù)凴uby的private和protected

標(biāo)簽:克拉瑪依 甘孜 西雙版納 棗莊 南昌 河池 大慶 內(nèi)江

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Ruby 之 class 中的 private、 protected、public》,本文關(guān)鍵詞  Ruby,之,class,中的,private,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Ruby 之 class 中的 private、 protected、public》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于Ruby 之 class 中的 private、 protected、public的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    望江县| 鄂尔多斯市| 梅河口市| 长沙县| 彭山县| 潼南县| 乐安县| 庆元县| 嘉定区| 溧水县| 海晏县| 恩施市| 吴桥县| 兴安县| 德清县| 桓仁| 荥阳市| 桃园县| 长宁区| 襄汾县| 公主岭市| 鹤壁市| 禄丰县| 眉山市| 舞阳县| 隆安县| 宁化县| 颍上县| 泰来县| 崇明县| 涿州市| 兴和县| 绥宁县| 文水县| 泾川县| 宜州市| 尚志市| 渝北区| 麟游县| 固原市| 抚宁县|