Python的类定义及初始化

''' Python的类定义及初始化'''
class Song(object):

    def __init__(self,lyrics):#这里类初始化函数需要四个下划线__init__
        self.lyrics = lyrics

    def sing_me_a_song(self):
        for line in self.lyrics:
            print(line)
'''类也是以缩进为单位,比如上面就是class Song的定义,下面就是主函数体了,用缩进来分割'''
happy_bday = 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"])
print('\n')
happy_bday.sing_me_a_song()
print('\n')
bulls_on_parade.sing_me_a_song()

使用WindowsPowershell运行结果:

![在这里插入图片描述]( https://img-blog.csdnimg.cn/20210305155551909.png?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NjYyODMxMQ==,size_16,color_FFFFFF,t_70)

posted @ 2021-03-05 16:00  Aesopd  阅读(247)  评论(0)    收藏  举报