习题 40 模块、类和对象

2017-10-14  本文已影响0人  南北东西总相随

习题 40 模块、类和对象

class Song(object):
    def __init__(self, lyrics):
        self.lyrics = lyrics

    def sing_me_a_song(self):
        for line in self.lyrics:
            print line

happy_baby = Song(["Happy birthday to you",
                   "I don't want to get sued",
                   "So I'll stop right there"])

bulls_on_parade = Song(["They rally around the family",
                        "With pockets full of shells"])

happy_baby.sing_me_a_song()

bulls_on_parade.sing_me_a_song()

结果:

Happy birthday to you
I don't want to get sued
So I'll stop right there
They rally around the family
With pockets full of shells
上一篇 下一篇

猜你喜欢

热点阅读