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-02-26 09:30:01 发布