When to Use Inheritance or Composition

来源:互联网 发布:一起走软件作弊 编辑:程序博客网 时间:2024/05/16 06:49

1 Avoid multiple inheritance at all costs, as it’s too complex to be reliable. If you’re stuck with it, then be prepared to know the class hierarchy and spend time finding where everything is coming from.避免使用多层次继承
2 Use composition to package code into modules that are used in many different unrelated places and situations.模块可用组合
3 Use inheritance only when there are clearly related reusable pieces of code that fit under a single common concept or if you have to because of something you’re using.

0 0
原创粉丝点击