美文网首页
习题 40 模块、类和对象

习题 40 模块、类和对象

作者: 南北东西总相随 | 来源:发表于2017-10-14 21:33 被阅读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

相关文章

网友评论

      本文标题:习题 40 模块、类和对象

      本文链接:https://www.haomeiwen.com/subject/mmifuxtx.html