一.组合
class Foo(object): def __init__(self,age): self.age = age def display(self): print(self.age) data_list = [Foo(8),Foo(9)] for item in data_list: print(item.age,item.display()) #结果:8 8 None 9 9 None
class StarkConfig(object): def __init__(self,num): self.num = num def changelist(self,request): print(self.num,request) class RoleConfig(StarkConfig): def changelist(self,request): print("666") config_obj_list = [StarkConfig(1),StarkConfig(2),RoleConfig(3)] for item in config_obj_list: print(item.num)
class StarkConfig(object): def __init__(self,num): self.num = num def changelist(self,request): print(self.num,request) class RoleConfig(StarkConfig): pass config_obj_list=[StarkConfig(1),StarkConfig(2),RoleConfig(3)] for item in config_obj_list: item.changelist(168) #结果:1 168 2 168 3 168
class StarkConfig(object): def __init__(self,num): self.num = num def changelist(self,request): print(self.num,request) class RoleConfig(StarkConfig): def changelist(self,request): print(666,self.num) config_obj_list=[StarkConfig(1),StarkConfig(2),RoleConfig(3)] for item in config_obj_list: item.changelist(168) #结果: 1 168 2 168 666 3
实例
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 site = AdminSite() print(len(site._registry)) site.register("range",666) site.register("shilei",438) print(len(site._registry)) #结果: 0 2
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 site = AdminSite() # print(len(site._registry)) # site.register("range",666) # site.register("shilei",438) # print(len(site._registry)) site.register("lyd",StarkConfig(19)) site.register("yjl",StarkConfig(20)) site.register("fgz",RoleConfig(33)) print(len(site._registry)) for k,row in site._registry.items(): row.changelist(5) #结果: 19 5 20 5 666 33
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 site = AdminSite() site.register("lyd",StarkConfig(19)) site.register("yjl",StarkConfig(20)) site.register("fgz",RoleConfig(33)) print(len(site._registry)) for k,row in site._registry.items(): row.run() #结果: 19 999 20 999 666 33
class Userlnfo(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) site = AdminSite() site.register(Userlnfo,StarkConfig) site.register(Department,StarkConfig) print(len(site._registry)) for k,row in site._registry.items(): row.run() #结果: Userlnfo类 999 # Department类 999
总结:
1.对象中封装了什么
2.self 到底是谁
二.主动调用其它类的成员
方式一:
class Base: def f1(self): print("5个功能") class Foo: def f1(self): print("3个功能") Base.f1(self) obj = Foo() obj.f1()
总结:
Base.实例方法(自己传self)
与继承无关
方式二:按照类的继承顺序,找下一个
class Foo: def f1(self): super().f1() print("3个功能") class Bar: def f1(self): print("5个功能") class lnfo(Foo,Bar): pass obj = lnfo() obj.f1() class Foo: def f1(self): print("5个功能") class Bar(Foo): def f1(self): super().f1() print("3个功能") obj = Bar() obj.f1()
三.特殊成员
1. __doc__
表示类的描述信息
1 class Foo: 2 """ 描述类信息,这是用于看片的神奇 """ 3 4 def func(self): 5 pass 6 7 print Foo.__doc__ 8 #输出:类的描述信息
2. 类名() 自动执行__init__ # 初始化方法
初始化数据,通过类创建对象时,自动触发执行。
class Foo: def __init__(self, name): self.name = name self.age = 18 obj = Foo('wupeiqi') # 自动执行类中的 __init__ 方法
3.对象() 自动执行__call__
class Foo(object): def __call__(self,*args,**kwargs): print(520,args,kwargs) return
obj = Foo() obj(3,6,5,k1= 582) #11111 (6, 4, 2) {'k1': 456}
4. 对象["xx"] 自动执行 __getitem__
class Foo (object):
def __getijtem__(self,item):
print(item):
return "ss"
obj = Foo()
ret = obj[ "wang"] print(ret) # wang ss
5.对象["ss"] = 11 自动执行__setitem__
class Foo(object) def _getitem__(self,key,value): print(key,value,520) obj = Foo() obj["shuzi"] = 666
# shuzi 666 520
6.del 对象[xx] 自动执行__delitem__
class Foo (object): def __delitem__(self,key): print(key) obj = Foo() del obj["wang"]
# wang
7.对象+对象 自动执行__add__
class Foo(object) def __init__(self,s1,s2): self.s1 = s1 self.s2 = s2 def__add__(self,other): return self.s1 + other.s2 obj1 = Foo(1,2) obj2 = Foo(88,99) ret = obj1 + obj2 print(ret)
# 100
8. with 对象 自动执行__enter__/ __exit__
class Foo(object): def __init__(self,s1,s2): self.s1 = s1 self.s2 = s2 def __enter__(self): print("520")
return 999 def __exit__(self,exc_type,exc_val,exc_tb): print("251") obj = Foo(1,2) with obj as f: print(f) print("代码块")
# 520 999 代码块 251
9. __new__ 构造方法
class Foo(object): def __init__(self, a1, a2): # 初始化方法 """ 为空对象进行数据初始化 :param a1: :param a2: """ self.a1 = a1 self.a2 = a2 def __new__(cls, *args, **kwargs): # 构造方法 """ 创建一个空对象 :param args: :param kwargs: :return: """ return object.__new__(cls)
10. __dict__ 类或对象中的所有成员
类的普通字段属于对象;类中的静态字段和方法等属于类,
class Province: country = 'China' def __init__(self, name, count): self.name = name self.count = count def func(self, *args, **kwargs): print 'func' # 获取类的成员,即:静态字段、方法、 print Province.__dict__ # 输出:{'country': 'China', '__module__': '__main__', 'func': <function func at 0x10be30f50>, '__init__': <function __init__ at 0x10be30ed8>, '__doc__': None} obj1 = Province('HeBei',10000) print obj1.__dict__ # 获取 对象obj1 的成员 # 输出:{'count': 10000, 'name': 'HeBei'} obj2 = Province('HeNan', 3888) print obj2.__dict__ # 获取 对象obj1 的成员 # 输出:{'count': 3888, 'name': 'HeNan'}
11. __iter__
用于迭代器,之所以列表、字典、元组可以进行for循环,是因为类型内部定义了 __iter__
class Foo(object): def __init__(self, sq): self.sq = sq def __iter__(self): return iter(self.sq) obj = Foo([11,22,33,44]) for i in obj: print i
创建类就可以有两种方式:
a). 普通方式
class Foo(object): def func(self): print 'hello wupeiqi'
b).特殊方式(type类的构造函数)
def func(self): print 'hello wupeiqi' Foo = type('Foo',(object,), {'func': func}) #type第一个参数:类名 #type第二个参数:当前类的基类 #type第三个参数:类的成员