免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4543 | 回复: 8
打印 上一主题 下一主题

简单的Ruby 类 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-07-22 09:54 |只看该作者 |倒序浏览
[转帖]来源:Linux社区  作者:cheungmine

建立下面的脚本文件,命名为C:\animals.rb,使用notepad++,选择格式为以UTF-8无BOM格式编码保存:
  1.     #模块声明, 类似于名称空间   
  2.     module Animals  
  3.       
  4.     class Animal  
  5.         # 类的默认构造函数   
  6.         def initialize(name, species)  
  7.             @name=name  
  8.             @species=species  
  9.         end  
  10.          
  11.         #getter for name attribute   
  12.         def name  
  13.             @name  
  14.         end  
  15.       
  16.         #setter for name attribute   
  17.         def  name=(nameVal)  
  18.             @name=nameVal  
  19.         end  
  20.          
  21.         #getter for species attribute   
  22.         def species  
  23.             @species  
  24.         end  
  25.       
  26.         #setter for species attribute   
  27.         def  species=(speciesVal)  
  28.             @species=speciesVal  
  29.         end  
  30.     end  
  31.       
  32.     # 类DogAnimal继承自Animal   
  33.     class DogAnimal < Animal  
  34.         def initialize(voice)  
  35.             super("Dog", "mammal")  
  36.             @voice = voice  
  37.         end  
  38.          
  39.         #getter for voice attribute   
  40.         def voice  
  41.             @voice  
  42.         end  
  43.       
  44.         #setter for voice attribute   
  45.         def  voice=(voiceVal)  
  46.             @voice=voiceVal  
  47.         end  
  48.     end  
  49.       
  50.     end #module Animals   
  51.       
  52.     cat = Animals::Animal.new("Cat", "mammal");  
  53.     dog = Animals::DogAnimal.new("WangWang");  
  54.       
  55.     3.times{  
  56.         print cat.name + " is a " + cat.species + "\n"  
  57.         print dog.name + "'s voice is: " + dog.voice + "\n"  
  58.     }  
  59.       
  60.     =begin  
  61.     #ruby命令行运行脚本   
  62.     ruby c:\animals.rb  
  63.       
  64.     结果显示:  
  65.     Cat is a mammal  
  66.     Dog's voice is WangWang  
  67.     Cat is a mammal  
  68.     Dog's voice is WangWang  
  69.     Cat is a mammal  
  70.     Dog's voice is WangWang  
  71.     =end
复制代码

论坛徽章:
0
2 [报告]
发表于 2011-07-25 13:54 |只看该作者
呃,都没人理吗?

论坛徽章:
0
3 [报告]
发表于 2011-07-25 18:12 |只看该作者
setter 和 getter 为啥不用attr_accessor ? attr_accessor能更简洁一些。

论坛徽章:
0
4 [报告]
发表于 2011-07-26 08:08 |只看该作者
入门的教程吧,先从简单概念阐述起。

论坛徽章:
0
5 [报告]
发表于 2011-07-26 10:20 |只看该作者
写的真好

论坛徽章:
0
6 [报告]
发表于 2011-07-26 12:43 |只看该作者
楼上貌似很久没出现了。

论坛徽章:
0
7 [报告]
发表于 2011-07-26 13:05 |只看该作者
,很久没来这了,但期间实践了一些啊,http://huoxy.me/

论坛徽章:
0
8 [报告]
发表于 2011-07-26 20:57 |只看该作者
哦,你的博客啊,挺不错的啊。

论坛徽章:
0
9 [报告]
发表于 2011-09-16 17:40 |只看该作者
ding
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP