我从本教程中遇到了以下示例:
class Song
@@plays = 0
def initialize(name, artist, duration)
@name = name
@artist = artist
@duration = duration
@plays = 0
end
def play
@plays += 1
@@plays += 1
"This song: #@plays plays. Total #@@plays plays."
end
end
s1 = Song.new("Song1", "Artist1", 234) # test songs
s2 = Song.new("Song2", "Artist2", 345)
puts s1.play
puts s2.play
puts s1.play
puts s1.play
@@plays 是否只能在 Song 类中礼貌地访问?该评论提出了不建议使用类变量的观点。是 b/c 它们在日常使用中通常不需要,并且在使用时会产生很多调试问题?