ClassB1()。test1() - ''覆盖方法1'' ClassB1()。test2() - ''基本方法2'' ClassA2()。test1() - ''基本方法1'' ClassA2()。test2() - ''覆盖方法2'' ClassB2() .test1() - ''覆盖方法1'' ClassB2()。test2() - ''覆盖方法2'' 现在为真正的测试 - class ClassC3(Class2,ClassB): def __init __(self): ClassB .__ init __(self ) def test1(self): print''从ClassC3覆盖方法1'' def test2(self): print''从ClassC3覆盖方法2'' ClassC3()。test1() - ''从ClassC3覆盖方法1'' ClassC3()。test2() - ''从ClassC3覆盖方法2 '' 所以它有效。但是,使用多重继承并不理想,class ClassB1(ClassB): def __init__(self): ClassB.__init__(self)class ClassB2(Class2,ClassB): def __init__(self): ClassB.__init__(self)Now if I do the following, I get the results shown, which is what Iwant -ClassA1().test1() - ''Base method 1''ClassA1().test2() - ''Base method 2''ClassB1().test1() - ''Overriding method 1''ClassB1().test2() - ''Base method 2''ClassA2().test1() - ''Base method 1''ClassA2().test2() - ''Overriding method 2''ClassB2().test1() - ''Overriding method 1''ClassB2().test2() - ''Overriding method 2''Now for the real test -class ClassC3(Class2,ClassB): def __init__(self): ClassB.__init__(self) def test1(self): print ''Overriding method 1 from ClassC3'' def test2(self): print ''Overriding method 2 from ClassC3''ClassC3().test1() - ''Overriding method 1 from ClassC3''ClassC3().test2() - ''Overriding method 2 from ClassC3''So it works. However, using multiple inheritance is not ideal, 为什么会这样?多重继承是一个非常有用的工具 - 但它很快就会变得棘手。恕我直言,它最好用于mixin课程......Why so ? Multiple inheritence is a pretty useful tool - but it canbecome tricky very soon. IMHO, it''s best use is for mixin classes... 和我 相信它甚至不支持某些语言。and Ibelieve it is not even supported in some languages. 在某些语言中甚至不支持很多东西! - )A lot of things aren''t even supported in some languages !-) 任何人都可以建议 a更好的解决这个问题的方法?Can anyone suggesta better way of tackling this problem? 不是我的帽子。关于Python和OO的一些注意事项:Python 是动态类型的,继承只是关于共享 的实现。还有另一种分享实施方式 - 组成/授权。它更灵活,可以避免笛卡儿式的产品。类的乘法。它的宣传也不如 继承 - 可能是因为某些语言而已。没有提供 任何支持。这里的好消息是,由于__getattr __ / __ setattr__钩子,Python让它变得轻而易举, 。现在我不知道是不是它b / b 有任何意义WRT /你当前的问题... - bruno desthuilliers python -c" print''@''。join([''。''。join([w [:: - 1] for p in p.split(''。'')) ]) p in''o **** @ xiludom.gro''split(''@'')])"Not out of my hat. Just a few considerations on Python and OO: Pythonbeing dynamically typed, inheritence is only about sharingimplementation. There''s another way to do share implementation -composition/delegation. It''s more flexible, and can avoid "cartesianproduct" multiplication of classes. It''s also less advertised thaninheritance - probably because of "some languages" that fail to offerany support for it. The good news here is that Python makes it a breeze,thanks to the __getattr__/__setattr__ hooks. Now I don''t know if itmakes any sense WRT/ your current problem...--bruno desthuillierspython -c "print ''@''.join([''.''.join([w[::-1] for w in p.split(''.'')]) forp in ''o****@xiludom.gro''.split(''@'')])" 这篇关于关于子类化的问题 - 版本2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-21 17:28