Everyday OOP Tasks in <Ruby Way>

本文介绍了Ruby中实现多重构造器的方法,通过类方法提供默认参数,并探讨了复杂类初始化时的两种方式:直接传递参数与使用块进行初始化。
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.
【评估多目标跟踪方法】9个高度敏捷目标在编队中的轨迹和测量研究(Matlab代码实现)内容概要:本文围绕“评估多目标跟踪方法”,重点研究9个高度敏捷目标在编队飞行中的轨迹生成与测量过程,并提供完整的Matlab代码实现。文中详细模拟了目标的动态行为、运动约束及编队结构,通过仿真获取目标的状态信息与观测数据,用于验证和比较不同多目标跟踪算法的性能。研究内容涵盖轨迹建模、噪声处理、传感器测量模拟以及数据可视化等关键技术环节,旨在为雷达、无人机编队、自动驾驶等领域的多目标跟踪系统提供可复现的测试基准。; 适合人群:具备一定Matlab编程基础,从事控制工程、自动化、航空航天、智能交通或人工智能等相关领域的研究生、科研人员及工程技术人员。; 使用场景及目标:①用于多目标跟踪算法(如卡尔曼滤波、粒子滤波、GM-CPHD等)的性能评估与对比实验;②作为无人机编队、空中交通监控等应用场景下的轨迹仿真与传感器数据分析的教学与研究平台;③支持对高度机动目标在复杂编队下的可观测性与跟踪精度进行深入分析。; 阅读建议:建议读者结合提供的Matlab代码进行实践操作,重点关注轨迹生成逻辑与测量模型构建部分,可通过修改目标数量、运动参数或噪声水平来拓展实验场景,进一步提升对多目标跟踪系统设计与评估的理解。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值