#3 楼 @ery 谢谢。 话说能不能举个例子(还是不大明白)
class Song
def initialize(name,artist,duration)
end
end
song=Song.new("Bicycle",:"Fleck",260)
puts song.to_s
class Song
def to_s
"Song:#{name}--#{artist}(#{duration})"
end
end
puts song.to_s
class KaraokeSong<Song
def initialize(name,artist,duration,lyrics)
super(name,artist,duration)
end
end
newsong=KaraokeSong.new("My Way","Sinatra",255,"And now,the...")
puts newsong.to_s
我把代码改成这个样子后
然后报错
‘rb:9:in to_s': undefined local variable or method
name' for #Song:0x9357bdc (NameError)”’
这个错误是什么意思。。。是因为没把局部变量变成实例变量吗。?
好吧。。我大概明白了