• 【面向对象】-类和对象作业


    1.类或对象是否可以做字典得key,可以但是不推荐

    class Person():
    pass

    dic={Person():123}

    print(dic)
    私有成员是如何实现的:改名策略

    # 作业

    # 1.类或对象是否能做字典的key 可以,不推荐
    """
    class Person():
    pass
    dic = {Person():123}
    print(dic)
    """
    # 2.简述python的私有成员是如何实现的 _改名策略
    # 3.私有成员能在类的外部使用么?能在子类中使用么? 都不行
    # 4.读程序写结果.(不执行)
    class StarkConfig(object):
    def __init__(self,num):
    self.num = num
    def changelist(self,request):
    print(self.num,request)

    class RoleConfig(StarkConfig):
    def changelist(self,request):
    print('666')

    # 创建了一个列表,列表中有三个对象(实例)
    config_obj_list = [StarkConfig(1),StarkConfig(2),RoleConfig(3)]
    for item in config_obj_list:
    print(item.num) # 1 2 3


    # 5.读程序写结果.(不执行)
    class StarkConfig(object):
    def __init__(self,num):
    self.num = num
    def changelist(self,request):
    print(self.num,request)

    class RoleConfig(StarkConfig):
    pass

    # 创建了一个列表,列表中有三个对象(实例)
    config_obj_list = [StarkConfig(1),StarkConfig(2),RoleConfig(3)]
    for item in config_obj_list:
    item.changelist(168)
    """
    1 168
    2 168
    3 168
    """
    # print(config_obj_list[0].num)


    # 6.读程序写结果.(不执行)
    class StarkConfig(object):
    def __init__(self,num):
    self.num = num
    def changelist(self,request):
    print(self.num,request)

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

    config_obj_list = [StarkConfig(1),StarkConfig(2),RoleConfig(3)]
    for item in config_obj_list:
    item.changelist(168)
    """
    1 168
    2 168
    666 3
    """


    # 7.读程序写结果.(不执行)
    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)

    config_obj_list = [StarkConfig(1),StarkConfig(2),RoleConfig(3)]
    config_obj_list[1].run() # 2 999
    config_obj_list[2].run() # 666 , 3


    # 8.读程序写结果.(不执行)
    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

    site = AdminSite()
    print(len(site._registry)) # 0
    site.register('range',666) # {range:666}
    site.register('shilei',438)# {range:666 , shilei:438}
    print(len(site._registry)) # 2

    site.register('lyd',StarkConfig(19))# {lyd:obj}
    site.register('yjl',StarkConfig(20))# {yjl:obj}
    site.register('fgz',RoleConfig(33)) # {fgz:obj}
    print(len(site._registry)) # 5
    print(site._registry)


    # 9.读程序写结果.(不执行)
    class StarkConfig():
    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():
    def __init__(self):
    self._registry = {}
    def register(self,k,v):
    self._registry[k] = v

    site = AdminSite()
    site.register('lyd',StarkConfig(19))
    site.register('yjl',StarkConfig(20))
    site.register('fgz',RoleConfig(33))
    """
    'lyd': <__main__.StarkConfig object at 0x0000029289477F28>,
    'yjl': <__main__.StarkConfig object at 0x0000029289477F60>,
    """
    for k,row in site._registry.items():
    row.changelist(5) # 19 5 / 20 5 / 666 33
    for k,row in site._registry.items():
    row.run() # 19 999 / 20 999 / 666 33
    """"""

    # 10.读程序写结果.(不执行)
    class JustCounter:
    __secretCount = 0
    def count(self):
    print(self.__secretCount) # 获取
    self.__secretCount += 1
    """
    self.__secretCount = self.__secretCount + 1
    self.__secretCount = 0 + 1 = 1
    self.__secretCount = 1 为当前对象添加私有成员__secretCount 值为1
    """

    def count3():
    print(JustCounter.__secretCount)
    JustCounter.__secretCount += 1
    print(JustCounter.__secretCount)

    class Bars(JustCounter):
    def count2(self):
    print(self.__secretCount)

    # 情况一
    """"""
    # counter1 = JustCounter()
    # counter1.count() # 0
    # counter1.count() # 1
    # JustCounter.count3() # 1

    """
    0
    1
    """
    # 情况二
    # counter2 = Bars()
    # counter2.count() # 1
    # counter2.count() # 2
    # 情况三
    # JustCounter.count3() # 0 1

  • 相关阅读:
    二分查找
    选择排序算法
    1.2.2凑零钱问题(暴力递归+动态规划)
    1.2.1斐波那契数列4种解法(暴力递归+动态规划)
    二维数组查找
    插入排序算法
    母亲的爱今天是母亲节,和妈妈通话后回忆起往事
    冒泡排序算法
    Pandas使用入门
    pandas.contact函数
  • 原文地址:https://www.cnblogs.com/bonnyxinxin/p/13026340.html
Copyright © 2020-2023  润新知