如何正確實現Ruby創(chuàng)建可參數化類
Ruby語言在實際使用中會創(chuàng)建許多類,來滿足我們的整體編程需求。對于初學者來說,我們必須熟練地掌握創(chuàng)建類的方法,比如Ruby創(chuàng)建可參數化類等等。#t#
如果我們要創(chuàng)建很多類,這些類只有類成員的初始值不同,我們很容易想起:
- class IntelligentLife # Wrong
way to do this! - @@home_planet = nil
- def IntelligentLife.home_planet
- @@home_planet
- end
- def IntelligentLife.home_planet=(x)
- @@home_planet = x
- end
- #...
- end
- class Terran < IntelligentLife
- @@home_planet = "Earth"
- #...
- end
- class Martian < IntelligentLife
- @@home_planet = "Mars"
- #...
- end
這種Ruby創(chuàng)建可參數化類方式是錯誤的,實際上Ruby中的類成員不僅在這個類中被所有對象共享,實際上會被整個繼承體系共享,所以我們調用Terran.home_planet,會輸出“Mars”,而我們期望的是Earth一個可行的方法:
我們可以通過class_eval在運行時延遲求值來達到目標:
- class IntelligentLife
- def IntelligentLife.home_planet
- class_eval("@@home_planet")
- end
- def IntelligentLife.home_planet=(x)
- class_eval("@@home_planet = #{x}")
- end
- #...
- end
- class Terran < IntelligentLife
- @@home_planet = "Earth"
- #...
- end
- class Martian < IntelligentLife
- @@home_planet = "Mars"
- #...
- end
- puts Terran.home_planet # Earth
- puts Martian.home_planet # Mars
最好的Ruby創(chuàng)建可參數化類方法:
我們不使用類變量,而是使用類實例變量:
- class IntelligentLife
- class << self
- attr_accessor :home_planet
- end
- #...
- end
- class Terran < IntelligentLife
- self.home_planet = "Earth"
- #...
- end
- class Martian < IntelligentLife
- self.home_planet = "Mars"
- #...
- end
- puts Terran.home_planet # Earth
- puts Martian.home_planet # Mars