class Song(object): # 创建一个叫Song的类
def __init__(self, lyrics): # 类Song有一个__init__函数,它接收self和lyrics作为参数
self.lyrics = lyrics
def sing_me_a_song(self): # 类Song有一个名为sing_me_a_song的函数,它接收self作为参数
for line in self.lyrics:
print(line) # 函数的功能是将self.lyrics中的参数line打印出来
happy_bday = Song(["Happy Birthday to you",
"I don't want to get sued",
"So I'll stop right there"]) # 将happy_bday设为类Song的一个实例
bulls_on_parade = Song(["They rally around the family",
"With pockets full of shells"]) # 将bulls_on_parade设为类Song的一个实例
happy_bday.sing_me_a_song() # 从happy_bday中找到sing_me_a_song函数,调用它
bulls_on_parade.sing_me_a_song() # 从bulls_on_parade中找到sing_me_a_song函数,调用它
本习题完成了对类的创建与使用,但还没完全整明白,先存档。