您的位置:首页 > 编程语言 > Python开发

learn python the hard way(笨办法学python) 练习40 类的用法

2016-01-25 11:09 253 查看
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函数,并调用它。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: