标签:aaa 理解 object fun lin 输出 pre div cti
# -*- coding:utf-8 -*-<br data-filtered="filtered"> class A(object): def __init__(self, xing, gender): # !#1 self.namea = "aaa" # !#2 self.xing = xing # !#3 self.gender = gender # !#4 self.funca() def funca(self): print("function a : %s" % self.namea) class B(A): def __init__(self, xing, age): # !#5 super(B, self).__init__(xing, age) # !#6(age处应为gender) self.nameb = "bbb" # !#7 ##self.namea="ccc" # !#8 ##self.xing = xing.upper() # !#9 self.age = age # !#10 def funcb(self): print("function b : %s" % self.nameb) def funca(self): print("(override)function a : %s" % self.namea) b = B("lin", 22) # !#11 print(b.nameb) print(b.namea) print(b.xing) # !#12 print(b.age) print(b.gender) # !#13 b.funcb() b.funca()
上面程序的输出结果为:
(override)function a : aaa bbb aaa lin 22 22 function b : bbb (override)function a : aaa
以上程序说明如果父类初始化方法中有调用了父类中定义的方法,恰好这个方法又被子类所覆盖,则super(B, self).__init__(xing, age)调用的父类初始化方法中调用的方法将是被覆盖后的方法。
标签:aaa 理解 object fun lin 输出 pre div cti
原文地址:https://www.cnblogs.com/Time-LCJ/p/9575330.html