鱼C论坛

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

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

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

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

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

x
  1. class CountList(list):
  2.     def __init__(self, *args):
  3.         [color=DarkRed]super().__init__(args)[/color]
  4.         self.count = []
  5.         for i in args:
  6.             self.count.append(0)

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

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

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

  15.     def __delitem__(self, key):
  16.         del self.count[key]
  17.         [color=Red]super().__delitem__(key)[/color]

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

  20.     def append(self, value):
  21.         self.count.append(0)
  22.      [color=Red]   super().append(value)[/color]

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

  26.     def remove(self, value):
  27.         key = super().index(value)
  28.         del self.count[key]
  29.       [color=Red]  super().remove(value)[/color]

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

  33.     def clear(self):
  34.         self.count.clear()
  35.         super().clear()

  36.     def reverse(self):
  37.         self.count.reverse()
  38.         super().reverse()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

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

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

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

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

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

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

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

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

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

  34.     def clear(self):
  35.         self.count.clear()
  36.         super().clear()

  37.     def reverse(self):
  38.         self.count.reverse()
  39.         super().reverse()

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

  41. 第二段代码不继承父类:
  42. class Mylist:
  43.     def __init__(self,*args):
  44.         self.values=[x for x in args]
  45.         value=[0 for x in args]
  46.         self.count=dict(zip(self.values,value))
  47.     def __len__(self):
  48.         return len(self.values)
  49.         
  50.     def __getitem__(self,key):
  51.         self.count[self.values[key]]+=1
  52.         return self.values[key]
  53.     def __setitem__(self,key,value):
  54.         self.values[key]=value

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

  58.     def counter(self,index):
  59.         return self.count[self.values[index]]
  60.         
  61.     def append(self,value):
  62.         self.values.append(value)
  63.         self.count[value]=0

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

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

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

  73.     def clear(self):
  74.         self.count.clear()
  75.         self.values.clear()

  76.     def reverse(self):
  77.         self.values.reverse()
  78.         

  79. list2=Mylist("one","two","there",5,4,3,"five","four")
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-3-31 19:19:18 | 显示全部楼层
看下它们的执行后的区别:
  1. 第一段代码执行了结果是这样的:
  2. >>> list1
  3. ['one', 'two', 'there', 5, 4, 3, 'five', 'four']
  4. 第二段代码没继承父类执行结果如下:
  5. >>> list2
  6. <__main__.Mylist object at 0x0000000002B34828>
  7. >>>
  8. >>>
  9. >>>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-12 20:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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