本文介绍了python:继承或组成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有,它使用 dict 的一些功能。我曾经将一个 dict 对象复合在一起,并从外部提供一些访问权限,但最近考虑到只是继承 dict 添加一些我可能需要的属性和方法。这是一个很好的方法,还是应该坚持撰写?

解决方案

继承经常被滥用。除非你的班级意图被用作具有额外功能的通用字典,否则我会说组合是要走的路。



保存转接通话通常不够好选择继承的原因。



从设计模式书中:

整个文字在这里:


Let's say that I have class, that uses some functionality of dict. I used to composite a dict object inside and provide some access from the outside, but recently thought about simply inheriting dict and adding some attributes and methods that I might require. Is it a good way to go, or should I stick to composition?

解决方案

Inheritance is very often abused. Unless your class is meant to be used as a generic dictionary with extra functionality, I would say composition is the way to go.

Saving forwarding calls is usually not a good enough reason for choosing inheritance.

From the Design Pattern book:

The entire text is here:http://blog.platinumsolutions.com/node/129

这篇关于python:继承或组成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 11:12