Python-面向对象-关于继承的题

Posted hsiyi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python-面向对象-关于继承的题相关的知识,希望对你有一定的参考价值。

题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-面向对象-关于继承的题的主要内容,如果未能解决你的问题,请参考以下文章

Python 面向对象 之 多继承 MRO

JS面向对象 关于类的封装继承多态

Python 面向对象 之 多继承 MRO

python 面向对象专题:继承

python 面向对象专题:继承

Python面向对象特性 - 继承