使用Python定義構造函數和析構函數
阿新 • • 發佈:2017-09-09
emp init log 刪除對象 使用 highlight cheng print 析構函數
#定義類 class Student: #名稱 name="張三"; #構造方法 def __init__(self): print ("----構造方法被調用----") #析構方法 def __del__(self): print("----析構方法被調用------") #自我介紹的方法 def show(self): print("你好:我是%s"%self.name) #實例化對象 zhangsan= Student() #輸出:----構造方法被調用---- # 刪除對象 del zhangsan #輸出:----析構方法被調用------
# 定義一個員工類 class Emploee: name="" age="" ID=0 #方法 def __init__(self,name,age): self.name=name self.age=age def show(self): #輸出多個變量 print ("我是%s;今年我%d"%(self.name,self.age)) #實例化一個對象 aibiancheng=Emploee("張三",18) aibiancheng.show()
使用Python定義構造函數和析構函數