class Song(object): #创建一个类Song,它是对象的一种 Make a class named Song that is-a object
def __init__(self, lyrics): #类Song有一个__init__接受self和lyrics作为参数 class Song has-a __init__ that takes self and lyrics parameters
self.lyrics = lyrics #从self中获取lyrics属性,并将其设为lyrics From self get the lyrics attribute and set it to lyrics
def sing_me_a_song(self): #类Song有一个函数名称为s..,它接受self作为参数 class Song has-a function named s..that takes self parameters
for line in self.lyrics: #for循环
print line
happy_bday = Song(["Happy birthday to you", #将happy_bday设为类Song的一个实例
"I don't want to get sued",
"So I'll stop right there"])
bulls_on_parade = Song(["They rally around tha family", #设为类Song的一个实例
"With pockets full of shells"])
happy_bday.sing_me_a_song() #从happy_bday中找到sing_me_a_song函数,并调用它。
bulls_on_parade.sing_me_a_song() #从bull_on_parade中找到sing_me_a_song函数,并调用它。learn python the hard way(笨办法学python) 练习40 类的用法
最新推荐文章于 2021-08-25 22:23:35 发布
本文介绍如何使用Python创建一个名为Song的类来存储歌词,并通过类的方法来实现演唱歌曲的功能。通过实例化Song类,可以轻松地创建不同风格的歌曲并进行演唱演示。
621

被折叠的 条评论
为什么被折叠?



