Ruby 之 class 中的 private、 protected、public
更新時(shí)間:2009年11月26日 19:09:47 作者:
Ruby 之 class 中的 private、 protected、public
Private
private 函數(shù)只能 在本類和子類的 上下文中調(diào)用,且只能通過(guò)self訪問(wèn)。
這個(gè)意思就是:private函數(shù),只能在本對(duì)象內(nèi)部訪問(wèn)到。
對(duì)象實(shí)例變量(@)的訪問(wèn)權(quán)限就是 private。
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。
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。
相關(guān)文章
Ruby中的block、proc、lambda區(qū)別總結(jié)
這篇文章主要介紹了Ruby中的block、proc、lambda區(qū)別總結(jié),本文講解了yield 和 block call 的區(qū)別、block 和 proc、lambda 的區(qū)別、proc 和 lambda 的區(qū)別,需要的朋友可以參考下2015-03-03使用Ruby編寫發(fā)送郵件的程序的簡(jiǎn)單教程
這篇文章主要介紹了使用Ruby編寫發(fā)送郵件的程序的簡(jiǎn)單教程,包括發(fā)送帶附件的郵件的代碼實(shí)例,需要的朋友可以參考下2015-04-04ruby實(shí)現(xiàn)github第三方認(rèn)證
GitHub在用戶認(rèn)證過(guò)程中采用了雙匙機(jī)制,在雙匙加密機(jī)制中,只有合法用戶才擁有私匙,只要GitHub在收到請(qǐng)求時(shí)可以證明提交請(qǐng)求的客戶端上擁有該私匙,即可以確認(rèn)該操作是由合法用戶發(fā)起的。我們通過(guò)ruby來(lái)簡(jiǎn)單模擬下吧。2015-06-06Ruby中實(shí)現(xiàn)統(tǒng)計(jì)文件行數(shù)、單詞數(shù)和字符數(shù)
這篇文章主要介紹了Ruby中實(shí)現(xiàn)統(tǒng)計(jì)文件行數(shù)、單詞數(shù)和字符數(shù),本文是自定義的一個(gè)函數(shù),需要的朋友可以參考下2015-01-01win7安裝ruby on rails開發(fā)環(huán)境
看到很多文章都說(shuō)ruby環(huán)境在windows上是非常難搭建,會(huì)出現(xiàn)各種各樣的怪問(wèn)題,所以都推薦到linux和mac上安裝開發(fā)。但是我按照教程搭了下,問(wèn)題也不算太多??傔^(guò)大概花費(fèi)了2個(gè)半小時(shí)左右就完成了。下面就把安裝的步驟及具體的版本記錄了一下供大家參考。2014-07-07詳解Ruby語(yǔ)言中的注釋用法與中文編碼問(wèn)題
這篇文章主要介紹了Ruby語(yǔ)言中的注釋用法與中文編碼問(wèn)題,是Ruby入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-10-10ruby、javascript、php中的觀察者模式實(shí)現(xiàn)代碼
這篇文章主要介紹了ruby、javascript、php中的觀察者模式實(shí)現(xiàn)代碼,通過(guò)本文可以深入的理解觀察者模式,需要的朋友可以參考下2014-11-11