Python之類的創建

class TheThing(object):
    """  __init__ 函數 用於創建內部變量 """
    def __init__(self):   
        self.number = 0

    """ self 類似於C++中的this """
    def some_fuction(self):
        print("I got called.")

    def add_me_up(self, more):
        self.number += more
        return self.number

# two different things
a = TheThing()
b = TheThing()

a.some_fuction()
b.some_fuction()

print(a.add_me_up(20))
print(b.add_me_up(20))
print(b.add_me_up(30))

print(a.number)
print(b.number)

 

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