python-class(3)
阿新 • • 發佈:2017-09-05
const com truct str ren utf clas parent cnblogs
1 #!/usr/bin/env python 2 #-*- coding:utf-8 -*- 3 ############################ 4 #File Name: class3.py 5 #Author: frank 6 #Email: [email protected] 7 #Created Time:2017-09-04 14:55:16 8 ############################ 9 10 class Parent: 11 parentAttr = 100 12 def __init__(self): 13 print"invoke construct of base class" 14 15 def parentMethod(self): 16 print "invode parentMethod" 17 18 def setAttr(self, attr): 19 Parent.parentAttr = attr 20 21 def getAttr(self): 22 print "attr of base class:", Parent.parentAttr 23 24 def myMethod(self): 25print ‘調用父類方法‘ 26 27 class Child(Parent): 28 def __init__(self): 29 print "invoke construct of sub class" 30 31 def childMethod(self): 32 print "invode childMethod" 33 34 def myMethod(self): 35 print ‘調用子類方法‘ 36 37 c = Child() # 實例化子類 38c.childMethod() # 調用子類的方法 39 c.parentMethod() # 調用父類方法 40 c.setAttr(200) # 再次調用父類的方法 - 設置屬性值 41 c.getAttr() # 再次調用父類的方法 - 獲取屬性值 42 c.myMethod()
python-class(3)