Python3:类定义及初始化
''' 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运行结果: