题1:
class F3(object): def f1(self): ret = super().f1() # 跳过本父类,执行下一个父类 print(ret) return 123 class F2(object): def f1(self): print(‘123‘) class F1(F3, F2): pass obj = F1() obj.f1() # 结果为:123 None
题2:
class F1(object): def __init__(self, a1): self.a1 = a1 def f2(self, arg): print(self.a1, arg) class F2(F1): def f2(self, arg): print(‘666‘) obj_list = [F1(1), F2(2), F2(3)] for obj in obj_list: obj.f2(‘Shawn‘) # 类名() 表示创建一个对象 此处F1(1).f2(‘Shawn‘) 便为一个对象 # 打印结果为:1 Shawn, 666, 666
题3:
class F1(object): def __init__(self, num): self.num = num def func(self, request): print(self.num, request) def run(self): self.func(999) # 这里会先去自己类里面去找 class F2(F1): def func(self, request): print(666, self.num) objs = [F1(1), F2(2), F2(3)] objs[1].run() objs[2].run() # 打印结果为:666 2, 666 3
题4:
class UserInfo(object): pass class Department(object): pass class StarkConfig(object): def __init__(self, num): self.num = num def changelist(self, request): print(self.num, request) def run(self): self.changelist(999) class RoleConfig(StarkConfig): def changelist(self, request): print(666, self.num) class AdminSite(object): def __init__(self): self._registry = {} def register(self, k, v): self._registry[k] = v(k) # self._registry[UserInfo]=StarkConfig(UserInfo) # self._registry[Department]=Roleconfig(Department) # 类名() 表示创建对象 site = AdminSite() site.register(UserInfo, StarkConfig) site.register(Department, RoleConfig) for k, row in site._registry.items(): row.run() # StarkConfig(UserInfo).run()和Roleconfig(Department).run() # 打印结果为:UserInfo 999, 666 Department
题5:
class UserInfo(object): pass class Department(object): pass class StarkConfig(object): def __init__(self, num): self.num = num def get_vals(self): v = [11, 22, 33] extra = self.extra_vals() if extra: v.extend(extra) return v # extra为空时返回v def extra_vals(self): # 第一次时,pass,返回给extra的为None pass def run(self): return self.get_vals() class RoleConfig(StarkConfig): def extra_vals(self): return [99, 88] class AdminSite(object): def __init__(self): self._registry = {} def register(self, k, v): self._registry[k] = v(k) site = AdminSite() site.register(UserInfo, StarkConfig) site.register(Department, RoleConfig) for k, row in site._registry.items(): print(row.run()) 结果为:[11, 22, 33], [11, 22, 33, 99, 88]
题6:
class StarkConfig(object): def __init__(self, num): self.num = num def __call__(self, *args, **kwargs): print(self.num) class RoleConfig(StarkConfig): def __call__(self, *args, **kwargs): print(self.num) v1 = StarkConfig(1) # 类名()-->创建对象 v2 = RoleConfig(11) v1() # 对象()-->会自动执行__call__ v2() # 结果为 1, 11
题7:
class StarkConfig(object): def __init__(self, num): self.num = num def run(self): # self为StarkConfig(1) self() # self为StarkConfig(1)()-->对象()-->自动执行__call__ def __call__(self, *args, **kwargs): print(self.num) # 1 class RoleConfig(StarkConfig): def __call__(self, *args, **kwargs): print(345) v1 = StarkConfig(1) v2 = RoleConfig(11) print(v1.run()) # StarkConfig(1).run() # 无返回值 print(v2.run()) # RoleConfig(11).run() # 结果为:1, None, 345, None
题8:
class StarkConfig(object): def __init__(self, num): self.num = num def run(self): self() def __call__(self, *args, **kwargs): print(self.num) class RoleConfig(StarkConfig): def __call__(self, *args, **kwargs): print(345) def __getitem__(self, item): return self.num[item] v1 = RoleConfig(‘alex‘) v2 = StarkConfig("wupeiqi") # print(v1[1]) # <-->RoleConfig(‘alex‘)[1] 对象[key]会自动执行__getitem__(self, item),key赋值给item # print(v2[2]) # 报错 # 结果:print(v1[1]) l
总结:
self在访问方法的顺序: 永远先找自己的. 自己的找不到再找父类的.
原文地址:https://www.cnblogs.com/Hsiyi/p/9937367.html
时间: 2024-10-24 00:15:39