如何在Python父類中指定需要在子類中重寫某些字段/方法?強制覆蓋字段/方法?
2
A
回答
4
你可以提出一個NotImplementedError
:
def my_method(self, arg):
raise NotImplementedError('Implement me')
有關屬性,你可以使用@property
裝飾:
@property
def my_property(self):
raise NotImplementedError('Implement me as well')
1
你可以看看abstract base class模塊。
但是,更簡單的替代方法是定義不執行任何操作的存根實現,或者提出NotImplementedError
。
相關問題
- 1. 強制覆蓋方法,如果另一種方法被覆蓋
- 2. 強制子類覆蓋父的方法
- 3. FFMPEG強制覆蓋
- 4. 覆蓋類字段的ToString()方法
- 5. 用無參數方法覆蓋字段
- 6. 在C#中「強制」覆蓋#
- 7. Kotlin強制覆蓋函數
- 8. 強制log4net覆蓋文件
- 9. Os.Rename中的強制覆蓋
- 10. Git強制覆蓋撤消?
- 11. 使用「非靜態」方法/字段覆蓋「靜態」方法/字段
- 12. 覆蓋admin.TabularInline字段
- 13. Java:強制子類覆蓋超類的方法
- 14. '覆蓋'/'強制'某個隱式分辨率的安全方法
- 15. 覆蓋方法
- 16. 覆蓋方法
- 17. 覆蓋方法
- 18. 覆蓋方法
- 19. 覆蓋'+'方法
- 20. Java方法覆蓋 - 「方法不會覆蓋超級方法...」
- 21. 控制器覆蓋Magento(現有方法未覆蓋)
- 22. 覆蓋get方法
- 23. 覆蓋Uploadify方法
- 24. 覆蓋printf方法
- 25. C++方法覆蓋
- 26. VideoJS覆蓋方法
- 27. C++覆蓋方法
- 28. 覆蓋internalFrameClosing方法
- 29. 從覆蓋方法
- 30. 覆蓋/新方法
可能的重複http://stackoverflow.com/questions/1151212/equivalent-of-notimplementederror-for-fields-in-python – LSerni