公司动态
4.Python面向对象
面向对象【Object-Oriented Programming OOP】三大特性封装、继承、多态。封装数据打包至一个对象或者同一种类型的函数汇总到一个类中。多态如下所示参数args可以是任何存在方法send的类。类class对一类事物的抽象模板用来封装共同的属性特征和方法行为。class 类名: # 类属性变量 # 类方法 # 规范类名使用大驼峰命名MyStudent、UserInfo类属性写在__init__外面属于类本身所有对象共享同一份数据。实例属性self.变量名属于单个对象每个实例互相独立。def func(args): args.send()函数 vs 方法的区别类中的函数称之为方法其余均称之为函数。类名规范首字母大写。self本质为参数即指向当前实例。不需要调用方显式指明该实参该实参是由python内部自主传递的。class Father: # def play(self): print(this is father play...) # 子类的继承寻找成员时优先在当前类寻找否则从左往右依次寻找直到找到为止。存在则放弃继续寻找 class Son(Father): # 初始化方法类被实例化时python内部自动调用该方法不需要显式指定 def __init__(self,username): self.username username # delay、timeout 参数名 def __init__(self, delay: float 2.0, timeout: int 10): Args: delay: 请求间隔(秒)尊重服务器负载 timeout: 单次请求超时时间(秒) self.delay delay self.timeout timeout self.session requests.Session() self.session.headers.update(self.DEFAULT_HEADERS) self._last_request_time 0 self._robots_cache {} def play(self): print(this is son {} play....format(self.username)) son Son(李进); son.play()实例方法class Son(Father): def play(self): #方法存在self形参表明该方法为实例方法 print(this is son {} play....format(self.username)) son Son(李进) son.school()类方法class Son(Father): classmethod def book(cls): #cls是指类信息非实例 print(fclazz {cls.clazz}) Son.school()静态方法class Son(Father): staticmethod def school(): #注意不能存在self形参 print(上学校...) Son.school()