如何正确实现Ruby创建可参数化类
创始人
2024-06-15 22:21:41
0

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  

 

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...
2012年第四季度互联网状况报... [[71653]]  北京时间4月25日消息,据国外媒体报道,全球知名的云平台公司Akamai Te...