extend

在類定義中引入module後,module中的方法成爲類的類方法。 
在類定義中用extend引入module。 
例如: 
module Base 
  def test 
    p "This is a class method!" 
  end 
end 

class Car 
  extend Base 
end

Car.test => "This is a class method!" 


發佈了20 篇原創文章 · 獲贊 3 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章