如何正确实现Ruby创建可参数化类

Ruby语言在实际使用中会创建许多类,来满足我们的整体编程需求。对于初学者来说,我们必须熟练地掌握创建类的方法,比如Ruby创建可参数化类等等。#t#

如果我们要创建很多类,这些类只有类成员的初始值不同,我们很容易想起:

  1. class IntelligentLife # Wrong 
    way to do this!   
  2. @@home_planet = nil   
  3. def IntelligentLife.home_planet   
  4. @@home_planet   
  5. end   
  6. def IntelligentLife.home_planet=(x)   
  7. @@home_planet = x   
  8. end   
  9. #...   
  10. end   
  11. class Terran < IntelligentLife   
  12. @@home_planet = "Earth"   
  13. #...   
  14. end   
  15. class Martian < IntelligentLife   
  16. @@home_planet = "Mars"   
  17. #...   
  18. end  

这种Ruby创建可参数化类方式是错误的,实际上Ruby中的类成员不仅在这个类中被所有对象共享,实际上会被整个继承体系共享,所以我们调用Terran.home_planet,会输出“Mars”,而我们期望的是Earth一个可行的方法:

我们可以通过class_eval在运行时延迟求值来达到目标:

 
 
 
  1. class IntelligentLife   
  2. def IntelligentLife.home_planet   
  3. class_eval("@@home_planet")   
  4. end   
  5. def IntelligentLife.home_planet=(x)   
  6. class_eval("@@home_planet = #{x}")   
  7. end   
  8. #...   
  9. end   
  10. class Terran < IntelligentLife   
  11. @@home_planet = "Earth"   
  12. #...   
  13. end   
  14. class Martian < IntelligentLife   
  15. @@home_planet = "Mars"   
  16. #...   
  17. end   
  18. puts Terran.home_planet # Earth   
  19. puts Martian.home_planet # Mars  

最好的Ruby创建可参数化类方法:

我们不使用类变量,而是使用类实例变量:

 
 
 
  1. class IntelligentLife   
  2. class << self   
  3. attr_accessor :home_planet   
  4. end   
  5. #...   
  6. end   
  7. class Terran < IntelligentLife   
  8. self.home_planet = "Earth"   
  9. #...   
  10. end   
  11. class Martian < IntelligentLife   
  12. self.home_planet = "Mars"   
  13. #...   
  14. end   
  15. puts Terran.home_planet # Earth   
  16. puts Martian.home_planet # Mars  

 

THE END