#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