Object-Oriented
class
Category is functionally similar assembly
Objects
Object is a class specific individual embodied
Instantiate an object three things happen
- Create an object in memory space
- Automatically performing
__init__
the method, and the target address to the self - Performing
__init__
the method code inside, its properties to the object space of the package
self
Position parameter, the address of the receiving object
Spatial structure
Object query attributes: class space object space ---- ---- parent
Query a class attribute: class parent class space ---
And local and global consistent
Still, only the right to use the object class attribute, no deletion and modification of power
The relationship between class and class
Dependencies
The method of the class name of a class or object is passed to another class of
## 依赖关系 class Elephant: def __init__(self,name): self.name = name def open(self,obj): print(f"{self.name}打开了{obj.name}冰箱") obj.open_door() def close(self,obj): print(f"{self.name}关闭了{obj.name}冰箱") obj.close_door() class Refrigerator: def __init__(self,name): self.name = name def open_door(self): print(f"{self.name}被打开了") def close_door(self): print(f"{self.name}被关闭了") e1 = Elephant('冰河') b1 = Refrigerator('火') e1.open(b1) e1.close(b1)
Combination of relationship
The object class attribute is encapsulated into another class object
## 组合关系 class Boy: def __init__(self,name): self.name = name def meet(self,girl=None): self.girl_friend = girl def have_dinner(self): print(f"{self.name}和{self.girl_friend.name}共进晚餐") self.girl_friend.shopping(self) class Girl: def __init__(self,name,age): self.name = name self.age = age def shopping(self,obj): print(f"{obj.name}和{self.name}一起去购物") yu = Boy('yu') en = Girl('en',22) yu.meet(en) yu.have_dinner() #en.shopping(yu)
Dependent combination plus
class GameRole: def __init__(self,name,hp,ad): self.name = name self.hp = hp self.ad = ad def equip_weapon(self,obj): self.wea_attack = obj def attack(self,obj): obj.hp-=self.ad print (f"{self.name}攻击了{obj.name},{obj.name}还剩下{obj.hp}滴血") class Weapon: def __init__(self,name,att_value): self.name = name self.att_value = att_value def weapon(self,obj1,obj2): obj2.hp = obj2.hp-obj1.ad-self.att_value print (f"{obj1.name}使用{self.name}攻击了{obj2.name},{obj2.name}还剩下{obj2.hp}滴血") ys = GameRole('亚索',2000,200) jie = GameRole('劫',1800,220) wj = Weapon('无尽之刃',70) ys.equip_weapon(wj) ys.wea_attack.weapon(ys,jie)
Inheritance
inherit
- You can use all methods and properties of the parent class
- Inheritance advantages: save code, to enhance coupling, makes the code more standardized
- Inherited Cons: coupling too, is not conducive to the code changes
- Single inheritance: Only one parent
- Multiple inheritance: a plurality of parent classes, the order of succession, in accordance with the order mro (the class name can be used directly mro point ())
如计算merge( [E,O], [C,E,F,O], [C] )
有三个列表 : ① ② ③
merge不为空,取出第一个列表列表①的表头E,进行判断
各个列表的表尾分别是[O], [E,F,O],E在这些表尾的集合中,因而跳过当前当前列表
取出列表②的表头C,进行判断
C不在各个列表的集合中,因而将C拿出到merge外,并从所有表头删除
merge( [E,O], [C,E,F,O], [C]) = [C] + merge( [E,O], [E,F,O] )
进行下一次新的merge操作 ......
---------------------
Ducks Model
Looks like a duck, is a duck
There is no relationship between the two classes, as long as the function is similar, consistent method name, this will be very easy to use
Normalization process
Examples of constraints normalization and class methods together
class Pay:
def pay(self,money):
raise NotImplementedError('sb 重写我')
class Qqpay(Pay):
def pay(self,money):
print(f'使用qq支付{money}')
class Alipay(Pay):
def pay(self,money):
print(f'使用支付宝支付{money}')
class Wechat(Pay):
def fuqian(self,money):
print(f"使用微信支付{money}")
# def pay(self,money):
# print(f"使用微信支付{money}")
def pay(obj,money):
obj.pay(money)
p1 = Alipay()
p2 = Qqpay()
p3 = Wechat()
pay(p1,100)
pay(p2,200)
pay(p3,300) #### 这里会报错
## 为了方便使用,相似的功能使用一种方法名,为了统一调用,使用def pay()统一接口
## 强制归一化,如果父类的pay方法不重写,那么执行pay传入对象就会报错
# 这种比较推荐
The second constraint
from abc import abstractmethod,ABCMeta
class Pay(metaclass=ABCMeta):
@abstractmethod
def pay(self,money):
...
class Qqpay(Pay):
def pay(self,money):
print(f'使用qq支付{money}')
class Alipay(Pay):
def pay(self,money):
print(f'使用支付宝支付{money}')
class Wechat(Pay):
def fuqian(self,money):
print(f"使用微信支付{money}")
# def pay(self,money):
# print(f"使用微信支付{money}")
def pay(obj,money):
obj.pay(money)
p1 = Alipay()
p2 = Qqpay()
p3 = Wechat() ## 这里就会报错
pay(p1,100)
pay(p2,200)
pay(p3,300)
## 使用java中抽象类的概念,个人理解,这样不太好,违背了python的包容性强的设计理念
super
super (A, self) Returns the next item in the list mro A method according to class dependent self
Namespaces
All in all namespaces python, as long as there is a hierarchical relationship (including relationships), small-scale namespaces all have the power to use a wide range of variables namespace, but no power to modify
And all values of the order, are looking to start their own, and then increasing the scope
Suddenly thought: the same memory in open space, we need the help of the power variable names point to be used, we only used, and no authority to modify; that is, memory open space can not be changed, change the variable name value only to open a space for the variable name to the new open space