zoukankan      html  css  js  c++  java
  • Python正课77 —— 多态

    本文内容皆为作者原创,如需转载,请注明出处:https://www.cnblogs.com/xuexianqi/p/12672288.html

    一:什么是多态

    同一事物有多种形态

    二:为何要有多态 ==> 多态会带来什么样的特性,多态性

    多态性指的是:可以在不考虑对象的具体类型情况下 直接使用对象

    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)
    

    Python中len的精妙之处

    # len('hello')
    # len([1, 2, 3])
    # len({'name': 'xxq', 'age': 18})
    
    # print('hello'.__len__())
    # print([1, 2, 3].__len__())
    # print({'name': 'xxq', 'age': 18}.__len__())
    
    
    def my_len(val):
        return val.__len__()
    
    
    print(my_len('hello'))
    print(my_len([1, 2, 3]))
    print(my_len({'name': 'xxq', 'age': 18}))
    

    三:鸭子类型

    # 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()
    obj2.read()
    obj3.read()
    

    四:了解知识点

    import abc
    
    # 指定metaclass属性将类设置为抽象类,抽象类本身只是用来约束子类的,不能被实例化
    class Animal(metaclass=abc.ABCMeta):  # 统一所有子类的方法
        @abc.abstractmethod     # 该装饰器限制子类必须定义有一个名为talk的方法
        def say(self):
            print('动物基本的发声...', end='')
    
    
    class People(Animal):   # 但凡继承Animal的子类都必须遵循Animal规定的标准
        pass
    
    
    class Dog(Animal):
        pass
    
    
    class Pig(Animal):
        pass
    
    
    obj1 = People()
    obj2 = Dog()
    obj3 = Pig()
    
    obj1.say()  # 动物基本的发声...卧槽
    obj2.say()  # 动物基本的发声...汪汪汪
    obj3.say()  # 动物基本的发声...吼吼吼
    
    # 若子类中没有一个名为talk的方法则会抛出异常TypeError,无法实例化
    # TypeError: Can't instantiate abstract class People with abstract methods say
    
    class Animal(metaclass=abc.ABCMeta):  # 统一所有子类的方法
        @abc.abstractmethod
        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()  # 动物基本的发声...吼吼吼
    
  • 相关阅读:
    初识python: random 模块
    初识python: 模块定义及调用
    JSON的stringify和parse方法
    微信小程序:日期组件picker的使用
    微信小程序:给data中对象中的属性设置值与给data中的属性或对象或数组设置值的区别
    微信小程序:点击预览大图功能
    微信小程序:解决小程序中有些格式如webpiPhone手机暂不支持的问题
    微信小程序:优化页面要渲染的属性
    微信小程序:标签字符串直接变成标签来显示要通过富文本技术
    微信小程序:添加全局的正在加载中图标效果
  • 原文地址:https://www.cnblogs.com/xuexianqi/p/12672288.html
Copyright © 2011-2022 走看看