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

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

创新互联建站是专业的新会网站建设公司,新会接单;提供网站设计、网站制作,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行新会网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

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

  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 

网站名称:如何正确实现Ruby创建可参数化类
转载来源:http://www.shufengxianlan.com/qtweb/news32/60482.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联