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): 25 print ‘调用父类方法‘ 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() # 实例化子类 38 c.childMethod() # 调用子类的方法 39 c.parentMethod() # 调用父类方法 40 c.setAttr(200) # 再次调用父类的方法 - 设置属性值 41 c.getAttr() # 再次调用父类的方法 - 获取属性值 42 c.myMethod()
时间: 2024-10-16 23:32:54