作者ihower (好2ˇ)
看板Ruby
标题Re: [问题] 关於Ruby的多型
时间Tue Apr 12 22:45:54 2011
※ 引述《ireullin (raison detre)》之铭言:
你这什麽招都不需要吧 XD 给个预设值即可:
class HtmlElement
def initialize(szTag, szValue=nil)
@arrEle = []
@hAtr = {}
@szTag = szTag
@arrEle.push(szValue) if szValue
end
end
Ruby 用任意参数列 method(*args) 来处理 function overloading
你可以参考我的投影片
http://ihower.tw/blog/archives/4797
: 小弟最近刚开始接触Ruby
: 所以有些语法还不太熟
: 下面这样写执行时会发生错误
: 请问Ruby没有提供多型的功能嘛
: 还是只有建构式如此呢
: class HtmlElement
: def initialize(szTag)
: @arrEle = Array.new
: @hAtr = Hash.new
: @szTag = szTag
: end
: def initialize(szTag, szValue)
: @arrEle= Array.new
: @hAtr = Hash.new
: @szTag = szTag
: @arrEle.push(szValue)
: end
: end
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 111.240.166.224
※ 编辑: ihower 来自: 111.240.166.224 (04/12 22:47)
※ 编辑: ihower 来自: 111.240.166.224 (04/12 22:48)