• 面向对象的日常笔记


    1.

    # Author:Jesi
    # Time : 2018/12/29 16:33
    class Base(object):
        def func(self):
            super(Base,self).func()
            print("Base.func")
    
    
    class Bar(object):
        def func(self):
            print("Bar.func")
    
    class Foo(Base,Bar):
        pass
    
    obj = Foo()
    obj.func()
    print(Foo.__mro__)  # super不是找父类,而是根据__mro__
    '''
    Bar.func
    Base.func
    (<class '__main__.Foo'>, <class '__main__.Base'>, <class '__main__.Bar'>, <class 'object'>)
    '''
    
    # =======================================
    obj = Base()
    obj.func()   # AttributeError: 'super' object has no attribute 'func'

    2.偏函数

    # Author:Jesi
    # Time : 2018/12/29 16:23
    import functools
    
    
    def index(a1, a2):
        return a1 + a2
    
    
    # 偏函数帮助开发者自动传递参数
    new_func = functools.partial(index, 666)
    
    ret = new_func(1)
    print(ret)  # 667

    3.继承

    # Author:Jesi
    # Time : 2018/12/29 16:27
    
    class Base(object):
        def func(self):
            print("base.func")
    
    
    
    class Foo(Base):
        def func(self):
    
            # 方式一:执行父类的func
            super(Foo,self).func()
    
            # 方式二:主动执行Base类的方法
            Base.func(self)
    
            # 执行真正的子类func
            print("Foo.func")
    
    obj = Foo()
    obj.func()
  • 相关阅读:
    MySQL group_concat() 函数用法
    Git error The file will have its original line endings in your working directory
    SQL exists 基本用法
    (11) 严格模式(use strict)
    (10)变量提升
    (9)调试
    (8)正则表达式
    (7)类型转换
    (6)typeof, null, 和 undefined
    (5)运算符
  • 原文地址:https://www.cnblogs.com/geogre123/p/10197236.html
Copyright © 2020-2023  润新知