1. 程式人生 > >面向對象的日常筆記

面向對象的日常筆記

面向 func 參數 tools bsp clas nbsp tool 面向對象

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()

面向對象的日常筆記