类YourC lass(BaseClassWant): 通过 Matt I think the noral way of doing that is to split the origional classinto two classes. What you have: class BaseClass(object): def method0:pass def method1:pass def method2:pass ... def method9:pass What you need:class BaseClassWant(object):def method0:pass def method1:pass ... def method4:pass class BaseClassDontWant(object):def method5:pass def method6:pass ... def method9:pass class BaseClass(BaseClassWant, BaseClassDontWant): # same as BaseClassabovepass class YourClass(BaseClassWant):passMatt 这意味着你所做的5包括不要依赖或打电话给你5 不要。否则你必须继承它们。然后你可以重构 吧: class X5YouDo:... class X5YouDont:... class X(X5YouDo,X5YouDont):... class Y(X5YouDo):... 如果你是寻找有限的能见度,Python没有它。 它只是手铐,并且做了你不能做的事情。 毕竟什么都不能阻止你的用户打电话: y = Y() X5YouDont.DontInheritMe(y,args) 来获取未经授权的方法。 That implies that the 5 you do include don''t rely on or call the 5 youdon''t. Otherwise you have to inherit them. Then you can refactorthem into: class X5YouDo: ...class X5YouDont: ...class X( X5YouDo, X5YouDont ): ...class Y( X5YouDo ): ... If you''re looking for restricted visibility, Python does not have it.It''s just handcuffs, and makes things you can''t do. After all, nothing would stop you user from calling: y= Y()X5YouDont.DontInheritMe( y, args ) to get at the uninherited methods. 这篇关于继承但只是部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-24 20:47