美文网首页
《笨办法学Python3》练习四十:模块,类和对象

《笨办法学Python3》练习四十:模块,类和对象

作者: 雨开Ame | 来源:发表于2019-03-07 13:10 被阅读0次

练习代码

class Song(object):

    def __init__(self, lyrics):
        self.lyrics = lyrics

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

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

print("===")

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

print("===")

bohemian_rhapsody = Song(["Is this the real life",
"Is this just fantasy",
"Caught in a landslide",
"No escape from reality"])

happy_bday.sing_me_a_song()

bulls_on_parade.sing_me_a_song()

bohemian_rhapsody.sing_me_a_song()

Study Drills

  1. Write some more songs using this and make sure you understand that you’re passing a list of strings as the lyrics.

  2. Put the lyrics in a separate variable, then pass that variable to the class to use instead.

  3. See if you can hack on this and make it do more things. Don’t worry if you have no idea how, just give it a try, and see what happens. Break it, trash it, thrash it, you can’t hurt it.

  4. Search online for “object-oriented programming” and try to overflow your brain with what you read. Don’t worry if it makes absolutely no sense to you. Half of that stuff makes no sense to me, too.

    Object-oriented programming - Wiki

相关文章

网友评论

      本文标题:《笨办法学Python3》练习四十:模块,类和对象

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