继承

class GradFather:

  def hehuajiu(self):

    pass

class Father(GradFather): #父类,基类

  def lanqiu(self):

    pass

  def zuqiu(self):

    pass

  def chouyan(self):

    pass

  def hejiu(self):

    pass

  def tangtou(self):

    pass

class Son(Father): #子类,派生类

  def baojian(self):

    pass

s = Son()

s.baojian()

s.hejiu()

#全部继承

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:inherit.py
# time:2021/02/22

class F:
    def f1(self):
        print('F.f1')
    def f2(self):
        print('F.f2')

class S:
    def s1(self):
        print('S.s1')

obj = S()
obj.s1()

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/inherit.py
S.s1

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:inherit.py
# time:2021/02/22

class F:
    def f1(self):
        print('F.f1')
    def f2(self):
        print('F.f2')

class S:
    def s1(self):
        print('S.s1')

obj = S()
obj.s1()
obj.f1()

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/inherit.py
S.s1
Traceback (most recent call last):
  File "/home/smoke/PycharmProjects/pythonProject/lean_python/inherit.py", line 19, in <module>
    obj.f1()
AttributeError: 'S' object has no attribute 'f1'

Process finished with exit code 1

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:inherit.py
# time:2021/02/22

class F:
    def f1(self):
        print('F.f1')
    def f2(self):
        print('F.f2')

class S(F):
    def s1(self):
        print('S.s1')

obj = S()
obj.s1()
obj.f1()

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/inherit.py
S.s1
F.f1

Process finished with exit code 0

#部分继承,重写

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:inherit.py
# time:2021/02/22

class F:
    def f1(self):
        print('F.f1')
    def f2(self):
        print('F.f2')

class S(F):
    def s1(self):
        print('S.s1')
    def f2(self):
        print('S.f2')
obj = S()
obj.s1()
obj.f2()

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/inherit.py
S.s1
S.f2

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:inherit.py
# time:2021/02/22

class F:
    def f1(self):
        print('F.f1')
    def f2(self):
        print('F.f2')

class S(F):
    def s1(self):
        print('S.s1')
    def f2(self):
        print('S.f2')

obj = S()
obj.s1()    #s1中的self是形参,此时代指obj
obj.f1()    #self永远指调用方法的调用者

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/inherit.py
S.s1
F.f1

Process finished with exit code 0

#即保留父类方法,又重写方法

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:inherit.py
# time:2021/02/22

class F:
    def f1(self):
        print('F.f1')
    def f2(self):
        print('F.f2')

class S(F):
    def s1(self):
        print('S.s1')
    def f2(self):
        super(S,self).f2()    #找到父亲的类,执行父亲类的f2方法
        print('S.f2')

obj = S()
obj.f2()

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/inherit.py
F.f2
S.f2

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:inherit.py
# time:2021/02/22

class F:
    def f1(self):
        print('F.f1')
    def f2(self):
        print('F.f2')

class S(F):
    def s1(self):
        print('S.s1')
    def f2(self):
        print('S.f2')
        super(S,self).f2()    #找到父亲的类,执行父亲类的f2方法


obj = S()
obj.f2()

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/inherit.py
S.f2
F.f2

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:inherit.py
# time:2021/02/22

class F:
    def f1(self):
        print('F.f1')
    def f2(self):
        print('F.f2')

class S(F):
    def s1(self):
        print('S.s1')
    def f2(self):
        print('S.f2')
        F.f2(self)    #另一种执行父类方法,但是推荐使用super()方法

obj = S()
obj.f2()

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/inherit.py
S.f2
F.f2

Process finished with exit code 0

5、面向对象三大特性之二:继承

1、继承

class 父类:

  pass

class 子类(父类):

  pass

2、重写

防止执行父类中的方法

3、self永远是执行该方法的调用者

4、

super(子类,self).父类中的方法()

父类名.父类中的方法(self,...)

#别人的应用程序,或web框架

class RequestHandler:
    def get(self,arg):
        print('为所欲为')

class BaseRequest(RequestHandler):
    def get(self,arg):
        print('...')
obj = RequestHandler()
obj.get('')

#多继承

class Father(GradFather): #父类,基类

  def 篮球(self):

    pass

  def 足球(slef):

    pass

class Father1(GradFather):

  def 篮球(self):

    pass

  def 足球(self):

    pass

class Son(Father,Father1): #子类,派生类,继承多个父类;

  def 保健(self):

  pass

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:inherit.py
# time:2021/02/22

class F1:
    def a(self):
        print('F1.a')

class F2:
    def a(self):
        print('F2.a')

class S(F1,F2):
    pass

obj = S()
obj.a()

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/inherit.py
F1.a

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:inherit.py
# time:2021/02/22

class F1:
    def a(self):
        print('F1.a')

class F2:
    def a(self):
        print('F2.a')

class S(F2,F1):
    pass

obj = S()
obj.a()

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/inherit.py
F2.a

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:inherit.py
# time:2021/02/22

class F0:
    def a(self):
        print('F0.a')

class F1(F0):
    def a(self):
        print('F1.a')

class F2:
    def a(self):
        print('F2.a')

class S(F1,F2):
    pass

obj = S()
obj.a()

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/inherit.py
F1.a

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:inherit.py
# time:2021/02/22

class F0:
    def a(self):
        print('F0.a')

class F1(F0):
    def a1(self):
        print('F1.a')

class F2:
    def a(self):
        print('F2.a')

class S(F1,F2):
    pass

obj = S()
obj.a()

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/inherit.py
F0.a

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:inherit.py
# time:2021/02/22

class F_1:
    def a(self):
        print('F_1.a')
class F0(F_1):
    def a1(self):
        print('F0.a')

class F1(F0):
    def a1(self):
        print('F1.a')

class F2:
    def a(self):
        print('F2.a')

class S(F1,F2):
    pass

obj = S()
obj.a()

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/inherit.py
F_1.a

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:inherit.py
# time:2021/02/22

class F_1:
    def a1(self):
        print('F_1.a')
class F0(F_1):
    def a1(self):
        print('F0.a')

class F1(F0):
    def a1(self):
        print('F1.a')

class F2:
    def a(self):
        print('F2.a')

class S(F1,F2):
    pass

obj = S()
obj.a()

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/inherit.py
F2.a

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:inherit.py
# time:2021/02/22

class Base:
    def a(self):
        print('Base.a')
class F0(Base):
    def a(self):
        print('F0.a')

class F1(F0):
    def a(self):
        print('F1.a')

class F2(Base):
    def a(self):
        print('F2.a')

class S(F1,F2):
    pass

obj = S()
obj.a()

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/inherit.py
F1.a

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:inherit.py
# time:2021/02/22

class Base:
    def a(self):
        print('Base.a')
class F0(Base):
    def a(self):
        print('F0.a')

class F1(F0):
    def a1(self):
        print('F1.a')

class F2(Base):
    def a(self):
        print('F2.a')

class S(F1,F2):
    pass

obj = S()
obj.a()

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/inherit.py
F0.a

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:inherit.py
# time:2021/02/22

class Base:
    def a(self):
        print('Base.a')
class F0(Base):
    def a1(self):
        print('F0.a')

class F1(F0):
    def a1(self):
        print('F1.a')

class F2(Base):
    def a(self):
        print('F2.a')

class S(F1,F2):    #交集父类继承,到达根时候执行F2
    pass

obj = S()
obj.a()

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/inherit.py
F2.a

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:inherit.py
# time:2021/02/22

class Base:
    def a(self):
        print('Base.a')
class F0(Base):
    def a1(self):
        print('F0.a')

class F1(F0):
    def a1(self):
        print('F1.a')

class F2(Base):
    def a1(self):
        print('F2.a')

class S(F1,F2):
    pass

obj = S()
obj.a()

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/inherit.py
Base.a

Process finished with exit code 0

5、Python中支持多继承

  a.左侧优先

  b.一条道走到黑

  c.如果有了同一个根时,根最后执行

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:inherit.py
# time:2021/02/22

class BaseReqest():
    pass

class RequestHandler(BaseReqest):
    def serve_forever(self):
        print('RequestHandler.serve_forever')
    def process_request(self):
        print('RequestHandler.process_request')

class Minx:
    def process_request(self):
        print('minx.process_request')

class Son(Minx,RequestHandler):
    pass

obj = Son()
obj.process_request()

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/inherit.py
minx.process_request

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:inherit.py
# time:2021/02/22

class BaseReqest():
    pass

class RequestHandler(BaseReqest):
    def serve_forever(self):
        print('RequestHandler.serve_forever')
    def process_request(self):
        print('RequestHandler.process_request')

class Minx:
    def process_request(self):
        print('minx.process_request')

class Son(Minx,RequestHandler):
    pass

obj = Son()
obj.serve_forever()

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/inherit.py
RequestHandler.serve_forever

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:inherit.py
# time:2021/02/22

class BaseReqest():
    pass

class RequestHandler(BaseReqest):
    def serve_forever(self):
        print('RequestHandler.serve_forever')
        self.process_request()
    def process_request(self):
        print('RequestHandler.process_request')

class Minx:
    def process_request(self):
        print('minx.process_request')

class Son(Minx,RequestHandler):
    pass

obj = Son()
obj.serve_forever()

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/inherit.py
RequestHandler.serve_forever
minx.process_request

Process finished with exit code 0

代码执行流程

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:inherit.py
# time:2021/02/22

class BaseRequest():
    def __init__(self):
        print('BaseRequest.init')

class RequestHandler(BaseRequest):
    def __init__(self):
        print('RequestHandler.init')

    def serve_forever(self):
        print('RequestHandler.serve_forever')
        self.process_request()

    def process_request(self):
        print('RequestHandler.process_request')

class Minx:
    def process_request(self):
        print('minx.process_request')

class Son(Minx,RequestHandler):
    pass

obj = Son() #init

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/inherit.py
RequestHandler.init

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:inherit.py
# time:2021/02/22

class BaseRequest():
    def __init__(self):
        print('BaseRequest.init')

class RequestHandler(BaseRequest):
    def __init__(self):
        print('RequestHandler.init')
        BaseRequest.__init__(self)

    def serve_forever(self):
        print('RequestHandler.serve_forever')
        self.process_request()

    def process_request(self):
        print('RequestHandler.process_request')

class Minx:
    def process_request(self):
        print('minx.process_request')

class Son(Minx,RequestHandler):
    pass

obj = Son() #init

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/inherit.py
RequestHandler.init
BaseRequest.init

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:inherit.py
# time:2021/02/22

class BaseRequest():
    def __init__(self):
        print('BaseRequest.init')

class RequestHandler(BaseRequest):
    def __init__(self):
        print('RequestHandler.init')
        BaseRequest.__init__(self)

    def serve_forever(self):
        print('RequestHandler.serve_forever')
        self.process_request()

    def process_request(self):
        print('RequestHandler.process_request')

class Minx:
    def process_request(self):
        print('minx.process_request')

class Son(Minx,RequestHandler):
    pass

obj = Son() #init
obj.serve_forever()

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/inherit.py
RequestHandler.init
BaseRequest.init
RequestHandler.serve_forever
minx.process_request

Process finished with exit code 0

知道代码执行流程后查看源码

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:inherit.py
# time:2021/02/22

import socketserver

obj = socketserver.ThreadingTCPServer(1,2)    #创建对象,执行init
obj.serve_forever()

socketserver源码执行流程

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:inherit.py
# time:2021/02/22

class BaseRequest():
    def __init__(self):
        print('BaseRequest.init')

class RequestHandler(BaseRequest):
    def __init__(self):
        print('RequestHandle.init')
        BaseRequest.__init__(self)
    def serve_forever(self):
        print('RequestHandler.serve_forever')
        self.process_request()

    def process_request(self):
        print('RequestHandler.process_request')

class Minx:
    def process_request(self):
        print('minx.process_request')

class Son(Minx,RequestHandler):
    pass

obj = RequestHandler()
obj.serve_forever()