• python基础----多态与多态性、super函数用法、继承原理


    一、多态与多态性                                                                       

    ㈠多态:

    多态指的是一类事物有多种形态,(一个抽象类有多个子类,因而多态的概念依赖于继承)

    1. 序列类型有多种形态:字符串,列表,元组。

    2. 动物有多种形态:人,狗,猪

     1 import abc
     2 class Animal(metaclass=abc.ABCMeta): #同一类事物:动物
     3     @abc.abstractmethod
     4     def talk(self):
     5         pass
     6 
     7 class People(Animal): #动物的形态之一:人
     8     def talk(self):
     9         print('say hello')
    10 
    11 class Dog(Animal): #动物的形态之二:狗
    12     def talk(self):
    13         print('say wangwang')
    14 
    15 class Pig(Animal): #动物的形态之三:猪
    16     def talk(self):
    17         print('say aoao')

    3. 文件有多种形态:文件文件,可执行文件

     1 import abc
     2 class File(metaclass=abc.ABCMeta): #同一类事物:文件
     3     @abc.abstractmethod
     4     def click(self):
     5         pass
     6 
     7 class Text(File): #文件的形态之一:文本文件
     8     def click(self):
     9         print('open file')
    10 
    11 class ExeFile(File): #文件的形态之二:可执行文件
    12     def click(self):
    13         print('execute file')

    4多态:同一种事物的多种形态,动物分为人类,猪类(在定义角度)

     1 class Animal:
     2     def run(self):
     3         raise AttributeError('子类必须实现这个方法')
     4 
     5 class People(Animal):
     6     def run(self):
     7         print('人正在走')
     8 
     9 class Pig(Animal):
    10     def run(self):
    11         print('pig is walking')
    12 
    13 class Dog(Animal):
    14     def run(self):
    15         print('dog is running')
    16 
    17 peo1=People()
    18 pig1=Pig()
    19 d1=Dog()
    20 
    21 peo1.run()
    22 pig1.run()
    23 d1.run()

    ㈡多态性:

    请务必注意注意注意:多态与多态性是两种概念.

    多态性:一种调用方式,不同的执行效果

    多态性是指具有不同功能的函数可以使用相同的函数名,这样就可以用一个函数名调用不同内容的函数。

    在面向对象方法中一般是这样表述多态性:向不同的对象发送同一条消息,不同的对象在接收时会产生不同的行为(即方法)。也就是说,每个对象可以用自己的方式去响应共同的消息。所谓消息,就是调用函数,不同的行为就是指不同的实现,即执行不同的函数。

    多态性分为静态多态性和动态多态性

    静态多态性:如任何类型都可以用运算符+进行运算

    动态多态性:如下

    1.

    2.

    >>> def func(animal): #参数animal就是对态性的体现
    ...     animal.talk()
    ... 
    >>> people1=People() #产生一个人的对象
    >>> pig1=Pig() #产生一个猪的对象
    >>> dog1=Dog() #产生一个狗的对象
    >>> func(people1) 
    say hello
    >>> func(pig1)
    say aoao
    >>> func(dog1)
    say wangwang

    3.

    >>> def func(f):
    ...     f.click()
    ... 
    >>> t1=Text()
    >>> e1=ExeFile()
    >>> func(t1)
    open file
    >>> func(e1)
    execute file

    4.

    # 多态性依赖于:
    #     1.继承
    #     2.如下
    ##多态性:定义统一的接口,
    def func(obj): #obj这个参数没有类型限制,可以传入不同类型的值
        obj.run() #调用的逻辑都一样,执行的结果却不一样
    
    func(peo1)
    func(pig1)
    
    func(d1)

    二、super函数用法                                                                    

     分别在python2和python3当中的用法

    在python2中:

     1 #coding:utf-8
     2 #super在python2中的用法:
     3     # 1:super(自己的类,self).父类的函数名字
     4     # 2:super只能用于新式类
     5 class People(object):
     6     def __init__(self,name,sex,age):
     7         self.name=name
     8         self.age=age
     9         self.sex=sex
    10     def walk(self):
    11         print('%s is walking' %self.name)
    12 class Chinese(People):
    13     country='China'
    14     def __init__(self,name,sex,age,language='Chinese'):
    15         # self.name=name
    16         # self.sex=sex
    17         # self.age=age
    18         # People.__init__(self,name,sex,age)
    19         super(Chinese,self).__init__(name,sex,age)
    20         self.language=language
    21 c=Chinese('egon','male',18)
    22 print c.name,c.age,c.sex,c.language

    在python3中:

     1 #super在python3中的用法:
     2 class People:
     3     def __init__(self,name,sex,age):
     4         self.name=name
     5         self.age=age
     6         self.sex=sex
     7     def walk(self):
     8         print('%s is walking' %self.name)
     9 class Chinese(People):
    10     country='China'
    11     def __init__(self,name,sex,age,language='Chinese'):
    12         # self.name=name
    13         # self.sex=sex
    14         # self.age=age
    15         # People.__init__(self,name,sex,age)
    16         super(Chinese,self).__init__(name,sex,age)
    17         self.language=language
    18     def walk(self,x):
    19         super().walk()
    20         print('子类的x',x)
    21 c=Chinese('egon','male',18)
    22 # print(c.name,c.age,c.sex,c.language)
    23 c.walk(123)

    三、继承原理                                                                             

    新式类的继承,在查找属性时遵循:广度优先 (python3中都是新式类)

    python2中经典类的继承,在查找属性时遵循:深度优先

    # coding:utf-8
    # 新式类的继承,在查找属性时遵循:广度优先
    class A(object):
        def test(self):
            print('from A')
        pass
    class B(A):
        # def test(self):
        #     print('from B')
        pass
    class C(A):
        # def test(self):
        #     print('from C')
        pass
    class D(B):
        # def test(self):
        #     print('from D')
        pass
    
    class E(C):
        # def test(self):
        #     print('from E')
        pass
    class F(D,E):
        # def test(self):
        #     print('from F')
        pass
    f1=F()
    # f1.test()
    
    # print(F.__mro__)       #只有新式类才有这个属性可以查看线性列表,经典类没有这个属性
    print(F.mro())           # mro(method resolution order):方法解析顺序
    
    # 广度优先:F->D->B->E->C->A->object
    
    
    
    
    
    
    
    
    #python2中经典类的继承,在查找属性时遵循:深度优先
    class A:
        # def test(self):
        #     print('from A')
        pass
    class B(A):
        # def test(self):
        #     print('from B')
        pass
    class C(A):
        # def test(self):
        #     print('from C')
        pass
    class D(B):
        # def test(self):
        #     print('from D')
        pass
    
    class E(C):
        # def test(self):
        #     print('from E')
        pass
    class F(D,E):
        # def test(self):
        #     print('from F')
        pass
    f1=F()
    f1.test()
    
    # F->D->B->A->E->C
    继承原理-例1
    #新式类的继承,在查找属性时遵循:广度优先
    class A(object):
        def test(self):
            print('from A')
        pass
    class X(A):
        # def test(self):
        #     print('from X')
        pass
    class B(X):
        # def test(self):
        #     print('from B')
        pass
    class C(A):
        # def test(self):
        #     print('from C')
        pass
    class D(B):
        # def test(self):
        #     print('from D')
        pass
    
    class E(C):
        # def test(self):
        #     print('from E')
        pass
    class F(D,E):
        # def test(self):
        #     print('from F')
        pass
    f1=F()
    f1.test()
    
    
    #广度优先:F->D->B->E->C->A->object
    继承原理-例2
    #新式类的继承,在查找属性时遵循:广度优先
    class X(object):
        # def test(self):
        #     print('from X')
        pass
    class Y(object):
        # def test(self):
        #     print('from Y')
        pass
    
    class B(X):
        # def test(self):
        #     print('from B')
        pass
    class C(Y):
        # def test(self):
        #     print('from C')
        pass
    class D(B):
        # def test(self):
        #     print('from D')
        pass
    
    class E(C):
        # def test(self):
        #     print('from E')
        pass
    class F(D,E):
    #     def test(self):
    #         print('from F')
        pass
    f1=F()
    f1.test()
    
    
    #F--->D---->B--->X--->E---->C---->Y---->object
    继承原理-例3
    #新式类的继承,在查找属性时遵循:广度优先
    class A(object):
        def test(self):
            print('from A')
        pass
    
    class B(A):
        # def test(self):
        #     print('from B')
        pass
    
    class C(A):
        # def test(self):
        #     print('from C')
        pass
    
    class D(A):
        # def test(self):
        #     print('from D')
        pass
    class E(B):
        # def test(self):
        #     print('from E')
        pass
    class F(C):
        # def test(self):
        #     print('from F')
        pass
    
    class G(D):
        # def test(self):
        #     print('from G')
        pass
    
    class H(E,F,G):
        # def test(self):
        #     print('from H')
        pass
    
    h1=H()
    h1.test()
    继承原理-例4
    class A:
        def fa(self):
            print('from A')
        def test(self):
            self.fa()
    class B(A):
        def fa(self):
            print('from B')
    b=B()
    b.test()      #b.test--->B--->A b.fa()
    继承原理-例5

    灵魂画师(矮根儿)的杰作:  。◕‿◕。

     

  • 相关阅读:
    亚马逊云服务器VPS Amazon EC2 免费VPS主机配置CentOS及其它内容
    Linux + Mono 目前已经支持Entity Framework 6.1
    CentOS上 Mono 3.2.8运行ASP.NET MVC4经验
    Linux CentOS下如何确认MySQL服务已经启动
    C#使用Timer.Interval指定时间间隔与指定时间执行事件
    MySQL数据库有外键约束时使用truncate命令的办法
    C++中字符和字符串的读取与使用
    结构体的运算符重载
    P1358 扑克牌
    P1284 三角形牧场
  • 原文地址:https://www.cnblogs.com/wangyongsong/p/6750436.html
Copyright © 2020-2023  润新知