Everyday OOP Tasks in <Ruby Way>

本文介绍了Ruby中实现多重构造器的方法,通过类方法提供默认参数,并探讨了复杂类初始化时的两种方式:直接传递参数与使用块进行初始化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Recently,I'm reading the <Ruby Way> book.Some people said that the book is suitable for someone who have some experience in Ruby and they must be right.Because there aren't any words to explain how to develop a ruby program and there are only some usage about Ruby hacks.

This blog is to blog some especial usage about OOP in Ruby.

[b]Using Multiple Constructors[/b]
There is no real Multiple Constructors in Ruby(Because there is not override concept in ruby).But the <Ruby Way> book supplies a way to implement Multiple Constructors.In fact,this way is very like a factory pattern.The following codes is a sample of Multiple Constructors.The codes show a rectangle can have two side lengths and three color values.We create additional class methods that assume certain defaults for some of the parameters.
[code]
class ColoredRectangle
def initialize(r,g,b,s1,s2)
@r,@g,@b,@s1,@s2 = r,g,b,s1,s2
end

def ColoredRectangle.whiteRect(s1,s2)
new(0xff, 0xff, 0xff, s1, s2)
end

def ColoredRectangle.grayRect(s1, s2)
new(0x88, 0x88, 0x88, s1, s2)
end

def ColoredRectangle.coloredSquare(r, g, b, s)
new(r, g, b, s, s)
end
def ColoredRectangle.redSquare(s)
new(0xff, 0, 0, s, s)
end

end[/code]

[b]More Elaborate Constructors[/b]

This is a very insteresting usage.When you create a complex class which need initialize a great number of instance variables in initialize methods,there are two method in ruby(but there is only one method in java except you implement all the setter methods of instance variables and call all the setter methods before you use the instance of this class).First,you can initialize all the instace variables in the initialize method and pass all the values in the arguments.Second,you can use the following way to initialize them.The way is to pass in a block to the initialize method.We can then evaluate the block in order to initialize the object.The following codes is a example about this:
[code]
class PersonalComputer
attr_accessor :manufacturer,:model,:processor,:clock,
:ram,:disk,:monitor,:colors,:vres,:hres,:net
def initialize(&block)
instance_eval &block;
end
end

desktop = PersonalComputer.new do
self.manufacturer = "Acme"
self.model = "THX-1138"
self.processor = "986"
self.clock = 2.4 # GHz
self.ram = 1024 # Mb
self.disk = 800 # Gb
self.monitor = 25 # inches
self.colors = 16777216
self.vres = 1280
self.hres = 1600
self.net = "T3"
end

p desktop.inspect
[/code]
If you run this code ,you will get the following result:
[code]"#<PersonalComputer:0x2bc7580 @colors=16777216, @clock=2.4, @net=\"T3\", @monitor=25, @processor=\"986\", @hres=1600, @disk=800, @model=\"THX-1138\", @vres=1280, @ram=1024, @manufacturer=\"Acme\">"[/code]

There are several things should be noted:
First of all,we're using accessors for our attributes so that we can assign values to them in an intuitive way.Second,the reference to self is necessary because a setter always takes an explicit receiver to distinguish the method call from an ordinary assignment to a local variable.Third,we should use instance_eval instead of eval in order to evaluate the block in the context of the object rather than that of the caller.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值