您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Python继承-如何禁用功能

Python继承-如何禁用功能

在Python中确实没有任何真正的“私有”属性方法。您可以做的一件事就是简单地覆盖子类中不需要的方法,并引发一个异常:

>>> class Foo( object ):
...     def foo( self ):
...         print 'FOO!'
...         
>>> class Bar( Foo ):
...     def foo( self ):
...         raise AttributeError( "'Bar' object has no attribute 'foo'" )
...     
>>> b = Bar()
>>> b.foo()
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
  File "<interactive input>", line 3, in foo
AttributeError: 'Bar' object has no attribute 'foo'
python 2022/1/1 18:37:04 有233人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶