鱼C论坛

 找回密码
 立即注册
查看: 3108|回复: 2

谁能跟我讲讲为什么要继承父类,什么意思呢。谢谢

[复制链接]
发表于 2017-3-31 14:31:33 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
class CountList(list):
    def __init__(self, *args):
        super().__init__(args)
        self.count = []
        for i in args:
            self.count.append(0)

    def __len__(self):
        return len(self.count)

    def __getitem__(self, key):
        self.count[key] += 1
        return super().__getitem__(key)

    def __setitem__(self, key, value):
        self.count[key] += 1
        super().__setitem__(key, value)

    def __delitem__(self, key):
        del self.count[key]
        super().__delitem__(key)

    def counter(self, key):
        return self.count[key]

    def append(self, value):
        self.count.append(0)
        super().append(value)

    def pop(self, key=-1):
        del self.count[key]
        return super().pop(key)

    def remove(self, value):
        key = super().index(value)
        del self.count[key]
        super().remove(value)

    def insert(self, key, value):
        self.count.insert(key, 0)
        super().insert(key, value)

    def clear(self):
        self.count.clear()
        super().clear()

    def reverse(self):
        self.count.reverse()
        super().reverse()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-3-31 19:13:58 | 显示全部楼层
我谈谈我的看法吧:1.因为继承了父类,才可以使用下面的super().xxx方法,省了很多事;2.继承了父类就可能直接通过实例名访问到列表了。关于第二点我给你两段代码作一个比较你就清楚了
第一段代码继承了父类:
class CountList(list):
    def __init__(self, *args):
        super().__init__(args)
        self.count = []
        for i in args:
            self.count.append(0)

    def __len__(self):
        return len(self.count)

    def __getitem__(self, key):
        self.count[key] += 1
        return super().__getitem__(key)

    def __setitem__(self, key, value):
        self.count[key] += 1
        super().__setitem__(key, value)

    def __delitem__(self, key):
        del self.count[key]
        super().__delitem__(key)

    def counter(self, key):
        return self.count[key]

    def append(self, value):
        self.count.append(0)
        super().append(value)

    def pop(self, key=-1):
        del self.count[key]
        return super().pop(key)

    def remove(self, value):
        key = super().index(value)
        del self.count[key]
        super().remove(value)

    def insert(self, key, value):
        self.count.insert(key, 0)
        super().insert(key, value)

    def clear(self):
        self.count.clear()
        super().clear()

    def reverse(self):
        self.count.reverse()
        super().reverse()

list1=CountList("one","two","there",5,4,3,"five","four")

第二段代码不继承父类:
class Mylist:
    def __init__(self,*args):
        self.values=[x for x in args]
        value=[0 for x in args]
        self.count=dict(zip(self.values,value))
    def __len__(self):
        return len(self.values)
        
    def __getitem__(self,key):
        self.count[self.values[key]]+=1
        return self.values[key]
    def __setitem__(self,key,value):
        self.values[key]=value

    def __delitem__(self,key):
        self.count.pop(self.values[key])
        self.values.remove(self.values[key])

    def counter(self,index):
        return self.count[self.values[index]]
        
    def append(self,value):
        self.values.append(value)
        self.count[value]=0

    def pop(self,index):
        self.count.pop(self.values[index])
        self.values.pop(index)        

    def remove(self,value):
        self.count.pop(value)
        self.values.remove(value)

    def insert(self,index,value):
        self.count[value]=0
        self.values.insert(index,value)

    def clear(self):
        self.count.clear()
        self.values.clear()

    def reverse(self):
        self.values.reverse()
        

list2=Mylist("one","two","there",5,4,3,"five","four")
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-3-31 19:19:18 | 显示全部楼层
看下它们的执行后的区别:
第一段代码执行了结果是这样的:
>>> list1
['one', 'two', 'there', 5, 4, 3, 'five', 'four']
第二段代码没继承父类执行结果如下:
>>> list2
<__main__.Mylist object at 0x0000000002B34828>
>>> 
>>> 
>>>
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-7 10:18

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表