1 class TestDate: 2 a = 1 3 4 # "__init__"為類的構造函數 5 def __init__(self): 6 self.a = 666 7 pass 8 9 def a_1(self): 10 print("a_1") 11 self.a_2() 12 13 def a_2(self): 14 print(self.a) 15 print("a_2") 16 17 18 if __name__ == "__main__": 19 # 實例化類的時候 需要加"()",如TestDate(),若只寫成TestDate 則類內部的方法無法相互調用 20 td = TestDate() 21 td.a_1()
輸出:
a_1
666
a_2