30、多态与鸭子类型

一、多态

1.1、什么是多态

  多态指的是同一事物有多重形态

  例如:动物有多种形态,相当于父子集

1.2、为什么要有多态

  多态具有多态性,即可以不考虑对象具体类型的状况下直接使用对象

class Animal: # 统一所有子类的方法
    def say(self):
        print('动物基本的发声频率。。。',end=' ')

class People(Animal):
    def say(self):
        super().say()
        print('嘤嘤嘤嘤嘤嘤嘤')

class Dog(Animal):
    def say(self):
        super().say()
        print('汪汪汪')

class Pig(Animal):
    def say(self):
        super().say()
        print('哼哼哼')


obj1=People()
obj2=Dog()
obj3=Pig()

#第一种直接调用方式
# obj1.say()
# obj2.say()
# obj3.say()

# 第二种调用方式,定义统一的接口,接收传入的动物对象
def animal_say(animal):
    animal.say()

animal_say(obj1)
animal_say(obj2)
animal_say(obj3)

1.3、抽象类调用(了解)

import abc

# 指定metaclass属性将类设置为抽象类,抽象类本身只是用来约束子类的,不能被实例化
class Animal(metaclass=abc.ABCMeta):
    @abc.abstractmethod # 该装饰器限制子类必须定义有一个名为talk的方法
    def talk(self): # 抽象方法中无需实现具体的功能
        pass

class Cat(Animal): # 但凡继承Animal的子类都必须遵循Animal规定的标准
    def talk(self):
        pass

cat=Cat() # 若子类中没有一个名为talk的方法则会抛出异常TypeError,无法实例化

1.4、鸭子类型

  指的是在功能相同的情况下,先将类进行定义,在通过定义的类直接调用功能

python推崇的是鸭子类型
# class Cpu:
#     def read(self):
#         print('cpu read')
#
#     def write(self):
#         print('cpu write')
#
# class Mem:
#     def read(self):
#         print('mem read')
#
#     def write(self):
#         print('mem write')
#
#
# class Txt:
#     def read(self):
#         print('txt read')
#
#     def write(self):
#         print('txt write')
#
#
# obj1=Cpu()
# obj2=Mem()
# obj3=Txt()
#
# obj1.read()
# obj1.write()
#
# obj2.read()
# obj2.write()
#
# obj3.read()
# obj3.write()

二、绑定方法和非绑定方法

2.1、绑定方法

扫描二维码关注公众号,回复: 10732088 查看本文章

  将调用者本身当做第一个参数传入

  绑定给对象:调用这是对象,自动传入的就是对象

  绑定给类:调用者是类,自动传入的就是类,可以使用装饰器的方式@classmethod,熊装饰器里面绑定类

  

import settings

class Mysql:
    def __init__(self,ip,port):
        self.ip=ip
        self.port=port

    def func(self):
        print('%s:%s' %(self.ip,self.port))   #调用类的方式

    @classmethod # 将下面的函数装饰成绑定给类的方法
    def from_conf(cls):
        print(cls)
        return cls(settings.IP, settings.PORT)

#obj1=Mysql('1.1.1.1',3306)     #绑定给对象的方式
obj2=Mysql.from_conf()          #绑定给类的调用方式     
print(obj2.__dict__)

2.2、非绑定的方式

  静态方法

  没有绑定给任何人,调用者可以是类或者对象,没有自动传参的效果,定义的是函数,不许要传参就能执行  

class Mysql:
    def __init__(self,ip,port):
        self.nid=self.create_id()
        self.ip=ip
        self.port=port

    @staticmethod # 将下述函数装饰成一个静态方法
    def create_id():
        import uuid     #函数内不需要参数就能执行
        return uuid.uuid4()

    @classmethod
    def f1(cls):
        pass

    def f2(self):
        pass
obj1=Mysql('1.1.1.1',3306)

# print(Mysql.create_id)
# print(obj1.create_id)

# Mysql.create_id(1,2,3)
# obj1.create_id(4,5,6)

print(Mysql.create_id)
print(Mysql.f1)
print(obj1.f2)

猜你喜欢

转载自www.cnblogs.com/jingpeng/p/12687407.html