Python-45-物件導向10.建立類別方法、呼叫類別方法

  • 建立類別方法
  • 呼叫類別方法
class cul_ticket:
    def __init__(self,order,fund,people): 
        self.order = order
        self.fund = fund
        self.people = people
       
    def info(self):  #類別方法 info函式 調用 class類別的變數
        print("訂購人:",self.order+"共買了",self.people,"張門票共",self.fund*self.people,"元")
    
    def info123(self,aaa):#第一個參數一定要放self(物件變數),否則程式會報錯
        print(aaa);
    

#呼叫方法1:
data = cul_ticket("John", 100,7)
data.info()#呼叫類別方法
data.info123(123)

#呼叫方法2:
data2 = cul_ticket("May", 100,2).info() 

看看效果

Yiru@Studio - 關於我 - 意如