python - 面向對象
#python面向對象 - 類定義
注意:特殊方法"__init__"前后分別有兩個下劃線!!!
__init__方法可以理解成定義屬性的方法,編輯器中會默認將屬性都綁定到self中,在使用時直接self.shuxing 即可喲;
__init__方法中第一位參數永遠是self,表示創建實例本身,記住!
1 class sjlx_03: 2 #構造方法 3 def __init__(self,name,sex): 4 self._name = name 5 self._sex = sex 6 7 def sayHello(self): 8 print("20190531:{0},{1}".format(self._name,self._sex)) 9 10 #如需使用類中方法需實例 11 s = sjlx_03("雙權","女") 12 s.sayHello()
打印結果:20190531:雙權,女
------------------------------------------------------------------------------------------------------
#python面向對象 - 繼承
1 #python面向對象 - 繼承 2 class hellow(sjlx_03): 3 def __init__(self,name): 4 self._name = name; 5 6 def sayHe(self): 7 print("hellow{0}".format(self._name)) 8 9 a = hellow("雙權01") 10 a.sayHe();
打印結果:hellow雙權01
浙公網安備 33010602011771號