class Song
def initialize(name,artist,duration)
@name =name
@artist =artist
@duration=duration
end
end
song=Song.new("Bicycle",:"Fleck",260)
puts song.to_s #<Song:0x1c8ac8>
class Song
def to_s
"Song:#@name--#@artist(#@duration)"
end
end
puts song.to_s #<Song:Bicycle--FLeck(260)>
class KaraokeSong<Song
def initialize(name,artist,duration,lyrics)
super(name,artist,duration)
@lyrics=lyrics
end
end
newsong=KaraokeSong.new("My Way","Sinatra",255,"And now,the...")
puts newsong.to_s #<Song:My Way--Sinatra(255)>
只是想问最后一个输出为什么不是 KaraokeSong:0x86e4a58 还有弱弱的问局部变量有什么用。。为什么要变成实例变量