1. 程式人生 > >Python-面向對象-關於繼承的題

Python-面向對象-關於繼承的題

val extend func bbb 技術分享 bject {} config sta

題1:

技術分享圖片
class F3(object):

    def f1(self):
        ret = super().f1()  # 跳過本父類,執行下一個父類
        print(ret)
        return 123


class F2(object):

    def f1(self):
        print(123)


class F1(F3, F2):
    pass


obj = F1()
obj.f1()   # 結果為:123 None
View Code

題2:

技術分享圖片
class F1(object):

    def
__init__(self, a1): self.a1 = a1 def f2(self, arg): print(self.a1, arg) class F2(F1): def f2(self, arg): print(666) obj_list = [F1(1), F2(2), F2(3)] for obj in obj_list: obj.f2(Shawn) # 類名() 表示創建一個對象 此處F1(1).f2(‘Shawn‘) 便為一個對象 # 打印結果為:1 Shawn, 666, 666
View Code

題3:

技術分享圖片
class F1(object):
    def __init__(self, num):
        self.num = num

    def func(self, request):
        print(self.num, request)

    def run(self):
        self.func(999)  # 這裏會先去自己類裏面去找


class F2(F1):

    def func(self, request):
        print(666, self.num)


objs = [F1(1), F2(2), F2(3)]
objs[
1].run() objs[2].run() # 打印結果為:666 2, 666 3
View Code

題4:

技術分享圖片
class UserInfo(object):
    pass


class Department(object):
    pass


class StarkConfig(object):
    def __init__(self, num):
        self.num = num

    def changelist(self, request):
        print(self.num, request)

    def run(self):

        self.changelist(999)


class RoleConfig(StarkConfig):

    def changelist(self, request):
        print(666, self.num)


class AdminSite(object):

    def __init__(self):
        self._registry = {}

    def register(self, k, v):
        self._registry[k] = v(k)  # self._registry[UserInfo]=StarkConfig(UserInfo)
                                   # self._registry[Department]=Roleconfig(Department)
                                   # 類名() 表示創建對象

site = AdminSite()
site.register(UserInfo, StarkConfig)
site.register(Department, RoleConfig)
for k, row in site._registry.items():
    row.run()  # StarkConfig(UserInfo).run()和Roleconfig(Department).run()

# 打印結果為:UserInfo 999,  666 Department         
View Code

題5:

技術分享圖片
class UserInfo(object):
    pass


class Department(object):
    pass


class StarkConfig(object):

    def __init__(self, num):
        self.num = num

    def get_vals(self):
        v = [11, 22, 33]

        extra = self.extra_vals()
        if extra:
            v.extend(extra)
        return v  # extra為空時返回v

    def extra_vals(self):  # 第一次時,pass,返回給extra的為None
        pass

    def run(self):

        return self.get_vals()


class RoleConfig(StarkConfig):

    def extra_vals(self):

        return [99, 88]


class AdminSite(object):

    def __init__(self):
        self._registry = {}

    def register(self, k, v):
        self._registry[k] = v(k)


site = AdminSite()
site.register(UserInfo, StarkConfig)
site.register(Department, RoleConfig)
for k, row in site._registry.items():
    print(row.run())
結果為:[11, 22, 33],  [11, 22, 33, 99, 88]
View Code

題6:

技術分享圖片
class StarkConfig(object):
    def __init__(self, num):
        self.num = num

    def __call__(self, *args, **kwargs):
        print(self.num)


class RoleConfig(StarkConfig):
    def __call__(self, *args, **kwargs):
        print(self.num)


v1 = StarkConfig(1)  # 類名()-->創建對象
v2 = RoleConfig(11)
v1()  # 對象()-->會自動執行__call__
v2()
# 結果為 1,  11
View Code

題7:

技術分享圖片
class StarkConfig(object):
    def __init__(self, num):
        self.num = num

    def run(self):  # self為StarkConfig(1)
        self()  # self為StarkConfig(1)()-->對象()-->自動執行__call__

    def __call__(self, *args, **kwargs):
        print(self.num)  # 1


class RoleConfig(StarkConfig):
    def __call__(self, *args, **kwargs):
        print(345)
        
             
v1 = StarkConfig(1)
v2 = RoleConfig(11)
print(v1.run())  # StarkConfig(1).run()  # 無返回值
print(v2.run())  # RoleConfig(11).run()
# 結果為:1,  None,  345, None
View Code

題8:

技術分享圖片
class StarkConfig(object):
    def __init__(self, num):
        self.num = num

    def run(self):
        self()

    def __call__(self, *args, **kwargs):
        print(self.num)


class RoleConfig(StarkConfig):

    def __call__(self, *args, **kwargs):
        print(345)

    def __getitem__(self, item):
        return self.num[item]


v1 = RoleConfig(alex)
v2 = StarkConfig("wupeiqi")
# print(v1[1])  # <-->RoleConfig(‘alex‘)[1] 對象[key]會自動執行__getitem__(self, item),key賦值給item
# print(v2[2])  # 報錯
# 結果:print(v1[1]) l
View Code

總結:

    self在訪問方法的順序: 永遠先找自己的. 自己的找不到再找父類的.

Python-面向對象-關於繼承的題