class Song
#属性
def name
@name
end
#构造/方法
def initialize(name)
@name=name
end
end
#继承
class Song2 < Song
#属性读写
attr_reader :artist
attr_writer :artist
#静态属性
@@play=0
#静态属性读写
def Song2.playcount
@@play
end
def Song2.playcount=(count)
@@play=count
end
#构造
def initialize(name,artist)
#基类构造
super(name)
@artist=artist
end
#方法
def playmethod
@@play+=1
end
#静态方法
def Song2.StaticPlay
@@play+=1
end
end
song2=Song2.new('yesterday once more','none')
song3=Song2.new('say you say me','none')
song2.playmethod #@@play=1
song3.playmethod #@@paly=2
Song2.StaticPlay #@@play=3
puts Song2.playcount #3
Song2.playcount=10
puts Song2.playcount #10
puts song3.name #say you say me
puts song3.artist #none
song3.artist='not me'
puts song3.artist #not me
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/blackant2/archive/2006/11/28/1418703.aspx
ruby学习笔记(1)--类的属性与方法
最新推荐文章于 2024-09-03 18:03:59 发布