免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 3861 | 回复: 12
打印 上一主题 下一主题

Python允许互相继承? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-04-20 15:52 |只看该作者 |倒序浏览
本帖最后由 千年沉寂 于 2010-04-21 09:31 编辑

如题,即A继承B,B继承A
code 1:
  1. class Mama(object):
  2.     def says(self):
  3.         print 'Do your homework'
  4. class Sister(Mama):
  5.     def method(self):
  6.         print 'Mama said'
  7.     def says(self):
  8.         super(Sister,self).says()
  9.         print 'and clean your bedroom'

  10. anita = Sister()
  11. anita.says()
复制代码
output 1:
Do your homework
and clean your bedroom

code 2:
  1. class Mama(Sister):
  2.     def says(self):
  3.         super(Mama,self).method()
  4.         print 'Do your homework'
  5. class Sister(Mama):
  6.     def method(self):
  7.         print 'Mama said'
  8.     def says(self):
  9.         super(Sister,self).says()
  10.         print 'and clean your bedroom'

  11. anita = Sister()
  12. anita.says()
复制代码
output 2:
Mama said
Do your homework
and clean your bedroom

直接运行第二段代码会出错,刚注意到。我用的是pyscripter做编辑器,python引擎是通过Rpyc调用解释解释执行。

刚开始是运行“code 1”得到“output 1”,把“code 1” 改成“code 2”,再运行,则正常,结果为“output 2”,即“code 3”可以直接运行,得到“output 2”

code 3
  1. class Mama(object):
  2.     def says(self):
  3.         print 'Do your homework'

  4. class Sister(Mama):
  5.     def method(self):
  6.         print 'Mama said'
  7.     def says(self):
  8.         super(Sister,self).says()
  9.         print 'and clean your bedroom'

  10. class Mama(Sister):
  11.     def says(self):
  12.         super(Mama,self).method()
  13.         print 'Do your homework'

  14. class Sister(Mama):
  15.     def method(self):
  16.         print 'Mama said'
  17.     def says(self):
  18.         super(Sister,self).says()
  19.         print 'and clean your bedroom'

  20. anita = Sister()
  21. anita.says()
复制代码

论坛徽章:
0
2 [报告]
发表于 2010-04-20 16:07 |只看该作者
不行吧   哪里看的?

论坛徽章:
0
3 [报告]
发表于 2010-04-20 17:10 |只看该作者
不会报错么。。

论坛徽章:
0
4 [报告]
发表于 2010-04-20 17:12 |只看该作者
回复 2# luffy.deng


    这段代码是可以运行的,Python允许多继承,联想到的 哈

论坛徽章:
0
5 [报告]
发表于 2010-04-20 17:13 |只看该作者
回复 3# smallfish_xy


    这段代码不会报错

论坛徽章:
0
6 [报告]
发表于 2010-04-20 17:19 |只看该作者
本帖最后由 luffy.deng 于 2010-04-20 17:23 编辑

回复 5# 千年沉寂

确定不会报错? 能找到name  Mama?

论坛徽章:
0
7 [报告]
发表于 2010-04-20 17:21 |只看该作者
回复  luffy.deng


    这段代码是可以运行的,Python允许多继承,联想到的 哈
千年沉寂 发表于 2010-04-20 17:12

多继承是可以的  但你这个不是多继承的问题。

论坛徽章:
0
8 [报告]
发表于 2010-04-20 19:04 |只看该作者
回复 7# luffy.deng

这是多继承必须处理的问题

论坛徽章:
0
9 [报告]
发表于 2010-04-20 21:57 |只看该作者
楼主在哪里执行不报错的?我在python2.6和3.1(改了print)中都会报:
NameError: name 'Sister' is not defined

论坛徽章:
0
10 [报告]
发表于 2010-04-21 08:58 |只看该作者
回复  luffy.deng

这是多继承必须处理的问题
千年沉寂 发表于 2010-04-20 19:04


为什么?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP