Python進階學習:類的實例

 

第一個類的 例:

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"])
    
bulls_on_parade = Song(["They rally around the family",
"With pockets full of shells"])

happy_bday.sing_me_a_song()
bulls_on_parade.sing_me_a_song()

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章