Ruby mix-in
Ruby 是一门面向对象的语言,所有一切都是对象包括基本数据类型变量(如:int,double,string,hash)等,函数,类型本身也是对象。Ruby在语言设计时没有采用C++等语言的多重继承,而是采用Java单继承的方式。也就是说每个类只能有一个父类,但是根据DRY的知道原则,多重继承在面向对象中是非常必要的。Java采用接口方式实现多重继承,Ruby则用mix-in方式实现。Java的interface不能定义具体的实现,mix-in方式是可以达到实现服用的效果的。
先看一下mix-in的具体实例
class Person
attr_accessor :name, :age, :gender
def initialize(name)
@name = name
end
end
module UsefulFeatures
def come_in(person)
self.current_person = person
#set_current_person(person)
end
def current_person
#self.current_person
@current_person
end
def set_current_person(person)
puts "set current person"
@current_person = person
end
end
class Room
include UsefulFeatures
def current_person=(person)
#self.current_person = person
puts "set current person (use = )"
@current_person = person
end
end
if __FILE__ == $0
jack = Person.new("jack")
puts "name: #{jack.name}"
bob = Person.new("bob")
room = Room.new()
room.come_in(jack)
puts "room current person #{room.current_person.name}"
end
运行结果:
> ruby test_mixin.rb
name: jack
set current person (use = )
room current person jack
可以看到例子中使用module定义一个类,在ruby中由module定义的类用
include module name
方式在其它类中使用。由module定义的类没有父类,也不能被实例话,因此就避免了复杂的继承树。
注:module中调用self.current_person = "..."实际上是调用Room的current=(person)方法。
如果将UsefulFeatures中的current_person改为
def current_person
self.current_person
#@current_person
end
运行脚本会得到如下错误
mix_in.rb:14: stack level too deep (SystemStackError)
因为在Ruby中所有一切都是对象,在ruby中不能直接访问类中的(实例)变量,所以self.current_person实际上调用的是current_person方法。因此current_person被调用无限次,所以报错。这就是一切皆是对象,当然只是其中一点,还有更多的有待与学习。
将UsefulFeatures中的come_in改为
def come_in(person)
#self.current_person = person
set_current_person(person)
end
再次运行程序得到
name: jack
set current person
room current person jack
直接使用UsefulFeatures中的set_current_person方法。mix-in实现的多重继承即避免的复杂的继承树,又达到了方法实现服用的目的。
分享到:
相关推荐
ruby186-25 for windows
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
Ruby,一种为简单快捷的面向对象编程(面向对象程序设计)而创的脚本语言,在20世纪90年代由日本人松本行弘(まつもとゆきひろ/Yukihiro Matsumoto)开发,遵守GPL协议和Ruby License。...该版本为ruby-1.8.7-p302
ruby-1.9.2-p290.tar.gz,ruby,redmin,安装,
ruby-irb-1.8.7.352-13.el6.x86_64.rpm ruby-irb-1.8.7.352-13.el6.x86_64.rpm
ruby-debug-ide-0.3.1.gem
ruby官网文件,但官网的安装包下载速度过于缓慢, 此安装包的版本是2.6.5-1-x64 如果您不知道要安装哪个版本,并且开始使用Ruby,建议您使用Ruby + Devkit 2.6.X(x64)安装程序。它提供了最多数量的兼容gem,并与...
该资源从官方下载,由于官方资源速度实在产不忍赌,特在此分流。
ruby开发环境傻瓜一键安装for windows
mingw32-ruby-1.9.1-wxruby-2.0.1-setup.exe
ruby-debug-base19-0.11.26.gem
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
ruby1-导出-1
官方离线安装包,亲测可用。使用rpm -ivh [rpm完整包名] 进行安装
0积分下载,不允许系统调分-20211115 ruby官方安装包
ruby对应数据库的必需gems包,包括最新的3个版本的dbi源码和gems安装包
ruby19-core.chm
博客“ruby+selenium-webdriver测试--第一个例子源代码”
ruby1-导出
A new Ruby folder is created in your C drive Ruby192 7. Download the selenium-client to c:\Ruby192\bin. The version I have downloaded was selenium-client 1.2.18 8. Set the CLASSPATH C:\Ruby192\bin –...