zoukankan      html  css  js  c++  java
  • 面向对象三大特性之多态

    面向对象三大特性之多态

    1 多态的定义

    多态就是同一事物有多种形态

    class Animal:
        pass
    
    class People(Animal):
        pass
    
    class Dog(Animal):
        pass
    
    class Pig(Animal):
        pass
    

    2 多态的作用

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

    class Animal: # 统一所有子类的方法
        def say(self):
            # 只要是animal的子类,就有say方法,不会报错
            pass
    
    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()
    
    
    

    使用abc模块可以强制子类必须含有某个方法的定义

    import abc
    
    # 指定metaclass属性将类设置为抽象类
    class Animal(metaclass=abc.ABCMeta): # 统一所有子类的标准
        @abc.abstractmethod		# 该装饰器限制子类必须定义有一个名为say的方法
        def say(self):
            pass
        
    obj=Animal() # 不能实例化抽象类自己
    
    class People(Animal):
        def say(self):
            pass
    
    class Dog(Animal):
        def say(self):
            pass
    
    class Pig(Animal):
        def say(self):
            pass
    
    pig = Pig() # 若子类中没有一个名为talk的方法则会抛出异常TypeError,无法实例化
    

    3 鸭子类型

    其实我们完全可以不依赖于继承,只需要制造出外观和行为相同对象,

    同样可以实现不考虑对象类型而使用对象,这正是Python崇尚的“鸭子类型”(duck typing):“如果看起来像、叫声像而且走起路来像鸭子,那么它就是鸭子”。

    比起继承的方式,鸭子类型在某种程度上实现了程序的松耦合度

    # 定义统一的接口,接收传入的人或狗或猪的对象
    # 使用这个方法便不再需要animal这个父类,这就是鸭子类型
    def say(animal):
        animal.say()
    
    say(obj1)
    say(obj2)
    say(obj3)
    # 这就是python中使用len()一个函数,就能计算列表,字符串,字典等不同数据类型的长度的原因
    
    # 使用鸭子类型写出自己的len()
    print('hello'.__len__())
    print([1,2,3].__len__())
    print({'a':1,'b':2}.__len__())
    
    def my_len(val):
        return val.__len__()
    
    print(my_len('hello'))
    print(my_len([1,2,3]))
    print(my_len({'a':1,'b':2}))
    
    
    # def len(val):
    #     return val.__len__()
    print(len('hello'))
    print(len([1,2,3]))
    print(len({'a':1,'b':2}))
    

    鸭子类型也蕴含着python之禅

    能不继承就不继承

    简单的总比复杂的好

    The Zen of Python, by Tim Peters
    
    Beautiful is better than ugly.
    Explicit is better than implicit.
    Simple is better than complex.
    Complex is better than complicated.
    Flat is better than nested.
    Sparse is better than dense.
    Readability counts.
    Special cases aren't special enough to break the rules.
    Although practicality beats purity.
    Errors should never pass silently.
    Unless explicitly silenced.
    In the face of ambiguity, refuse the temptation to guess.
    There should be one-- and preferably only one --obvious way to do it.
    Although that way may not be obvious at first unless you're Dutch.
    Now is better than never.
    Although never is often better than *right* now.
    If the implementation is hard to explain, it's a bad idea.
    If the implementation is easy to explain, it may be a good idea.
    Namespaces are one honking great idea -- let's do more of those!
    
    
  • 相关阅读:
    AOP 学习
    微服务架构理解[架构图](转)
    C# TSC打印二维码和条形码(转)
    C#注册表操作类--完整优化版(转)
    C#调用TSC条码打印机打印二维码(转)
    C#调用TSC条码打印机打印条码(转)
    TSC打印机使用教程终极版(转)
    海尔电商峰值系统架构设计最佳实践(转)
    亿级Web系统搭建——单机到分布式集群(转)
    数据库扩展性设计:使用二进制解决一条记录关联多个状态的问题(转),可以尝试一下
  • 原文地址:https://www.cnblogs.com/achai222/p/12685181.html
Copyright © 2011-2022 走看看