類的定義:
class 類名
??? def 方法名
??? ?? ...
??? end
end
類的實(shí)例化(申明對(duì)象):
對(duì)象名 = 類.new
繼承:
class 子類名<超類名
??? def 方法名
??? ?? ...
??? end
end
super:調(diào)用超類方法。也可以向原有的方法傳遞參數(shù)。
單態(tài)方法:特定對(duì)象的特定方法
ruby>?class?SingletonTest
????|???def?size
????|?????print?"25\n"
????|???end
????|?end
???nil
ruby>?test1?=?SingletonTest.new
???#<SingletonTest:0xbc468>
ruby>?test2?=?SingletonTest.new
???#<SingletonTest:0xbae20>
ruby>?def?test2.size
????|???print?"10\n"
????|?end
???nil
ruby>?test1.size
25
???nil
ruby>?test2.size
10
???nil