• Python--多态与多态性、绑定方法与非绑定方法


    多态与多态性

    多态

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

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

    s='hello'
    l=[1,2,3]
    t=('a','b','c')
    
    def check_len(obj):
        return len(obj)
    
    print(check_len(s))
    print(check_len(l))
    print(check_len(t))
    运行结果:
    5
    3
    3

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

    import abc
    class Animal(metaclass=abc.ABCMeta): #同一类事物:动物
        @abc.abstractmethod
        def talk(self):
            pass
    
    class People(Animal): #动物的形态之一:人
        def talk(self):
            print('say hello')
    
    class Dog(Animal): #动物的形态之二:狗
        def talk(self):
            print('say wangwang')
    
    class Pig(Animal): #动物的形态之三:猪
        def talk(self):
            print('say aoao')
    p=People()
    d=Dog()
    p2=Pig()
    p.talk()
    d.talk()
    p2.talk()


    运行结果:
    say hello
    say wangwang
    say aoao

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

    import abc
    class File(metaclass=abc.ABCMeta): #同一类事物:文件
        @abc.abstractmethod
        def click(self):
            pass
    
    class Text(File): #文件的形态之一:文本文件
        def click(self):
            print('open file')
    
    class ExeFile(File): #文件的形态之二:可执行文件
        def click(self):
            print('execute file')
    
    t=Text()
    e=ExeFile()
    t.click()
    e.click()
    运行结果:
    open file
    execute file

    多态性(注意:多态与多态性是两种概念。

    什么是多态性

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

    class Animal: #同一类事物:动物
        def talk(self):
            pass
    
    class People(Animal): #动物的形态之一:人
        def talk(self):
            print('say hello')
    
    class Dog(Animal): #动物的形态之二:狗
        def talk(self):
            print('say wangwang')
    
    class Pig(Animal): #动物的形态之三:猪
        def talk(self):
            print('say aoao')
    
    
    def func(Animal):  # 参数animal就是多态性的体现
        Animal.talk()
    
    people1=People() #产生一个人的对象
    pig1=Pig() #产生一个猪的对象
    dog1=Dog() #产生一个狗的对象
    
    func(people1)
    func(pig1)
    func(dog1)
    运行结果:
    say hello
    say aoao
    say wangwang
    import abc
    class File(metaclass=abc.ABCMeta): #同一类事物:文件
        @abc.abstractmethod
        def click(self):
            pass
    
    class Text(File): #文件的形态之一:文本文件
        def click(self):
            print('open file')
    
    class ExeFile(File): #文件的形态之二:可执行文件
        def click(self):
            print('execute file')
    
    def func(f):
        f.click()
    
    t=Text()
    e=ExeFile()
    func(t)
    func(e)
    运行结果:
    open file
    execute file

     综上我们也可以说,多态性是‘一个接口(函数func),多种实现(如f.click())’

    二 为什么要用多态性(多态性的好处)

    1.增加了程序的灵活性

      以不变应万变,不论对象千变万化,使用者都是同一种形式去调用,如func(animal)

    2.增加了程序可扩展性

    通过继承animal类创建了一个新的类,使用者无需更改自己的代码,还是用func(animal)去调用 

    >>> class Cat(Animal): #属于动物的另外一种形态:猫
    ...     def talk(self):
    ...         print('say miao')
    ... 
    >>> def func(animal): #对于使用者来说,自己的代码根本无需改动
    ...     animal.talk()
    ... 
    >>> cat1=Cat() #实例出一只猫
    >>> func(cat1) #甚至连调用方式也无需改变,就能调用猫的talk功能
    say miao
    
    '''
    这样我们新增了一个形态Cat,由Cat类产生的实例cat1,使用者可以在完全不需要修改自己代码的情况下。使用和人、狗、猪一样的方式调用cat1的talk方法,即func(cat1)
    '''

    绑定方法与非绑定方法

    类中定义的函数分成两大类:

      一:绑定方法(绑定给谁,谁来调用就自动将它本身当作第一个参数传入):

        1. 绑定到类的方法:用classmethod装饰器装饰的方法。

                    为类量身定制

                    类.boud_method(),自动将类当作第一个参数传入

                  (其实对象也可调用,但仍将类当作第一个参数传入)

        2. 绑定到对象的方法:没有被任何装饰器装饰的方法。

                   为对象量身定制

                   对象.boud_method(),自动将对象当作第一个参数传入

                 (属于类的函数,类可以调用,但是必须按照函数的规则来,没有自动传值那么一说)

    二:非绑定方法:用staticmethod装饰器装饰的方法

         1. 不与类或对象绑定,类和对象都可以调用,但是没有自动传值那么一说。就是一个普通工具而已

        注意:与绑定到对象方法区分开,在类中直接定义的函数,没有被任何装饰器装饰的,都是绑定到对象的方法,可不是普通函数,对象调用该方法会自动传值,而staticmethod装饰的方法,不管谁来调用,都没有自动传值一说

     

    1 staticmethod

     

    statimethod不与类或对象绑定,谁都可以调用,没有自动传值效果,python为我们内置了函数staticmethod来把类中的函数定义成静态方法

    import hashlib
    import time
    class MySQL:
        def __init__(self,host,port):
            self.id=self.create_id()
            self.host=host
            self.port=port
        @staticmethod
        def create_id(): #就是一个普通工具
            m=hashlib.md5(str(time.clock()).encode('utf-8'))
            return m.hexdigest()
    
    
    print(MySQL.create_id) #<function MySQL.create_id at 0x0000000001E6B9D8> #查看结果为普通函数
    conn=MySQL('127.0.0.1',3306)
    print(conn.create_id) #<function MySQL.create_id at 0x00000000026FB9D8> #查看结果为普通函数

    2 classmethod

              classmehtod是给类用的,即绑定到类,类在使用时会将类本身当做参数传给类方法的第一个参数(即便是对象来调用也会将类当作第一个参数传入),python为我们内置了函数classmethod来把类中的函数定义成类方法

    settings.py文件内容:

    HOST='127.0.0.1'
    PORT=3306
    from conf import settings
    class MySQL:
        def __init__(self,host,port):
            self.host=host
            self.port=port
    
        @classmethod
        def from_conf(cls):
            #print(cls)
            return cls(settings.HOST,settings.PORT)
    
    print(MySQL.from_conf) #<bound method MySQL.from_conf of <class '__main__.MySQL'>>conn=MySQL.from_conf()
    print(conn.host,conn.port)
    conn.from_conf() #对象也可以调用,但是默认传的第一个参数仍然是类
    运行结果:
    <bound method MySQL.from_conf of <class '__main__.MySQL'>>
    <class '__main__.MySQL'>
    127.0.0.1 3306
    <class '__main__.MySQL'>

     

    classmethod是可调用对象,可以当做装饰器使用

    非绑定方法:

    绑定到类的方法:

     

  • 相关阅读:
    springboot idea新建工程Initialization failed for 'https://start.spring.io' Please check URL, network and proxy settings
    TCP的三次握手与四次挥手理解及面试题
    lambda用法
    深入理解HashMap的扩容机制
    数据库连接池性能测试对比
    SQL
    AOP实现的统一日志处理
    阿里面试1
    汇总
    如果使用association和collection实现层级查询
  • 原文地址:https://www.cnblogs.com/metianzing/p/7132357.html
Copyright © 2020-2023  润新知