The purpose of interfaces continued

后端 未结 13 1396
轮回少年
轮回少年 2020-12-03 02:28

OK so I gather that Interfaces are a way to enforce that an object implements a certain amount of functionality, without having to use inheritance. Kind of like a contract.

相关标签:
13条回答
  • 2020-12-03 02:58

    Prefer Composition over Inheritance. This way, you can implement (say) eat() in a class that gets incorporated into all your animals as a data member. Write it once, reuse it, but in a way that doesn't bind one kind of functionality explicitly to another.

    If you had two (or ten) different ways of eating, you could swap them out as needed.

    0 讨论(0)
提交回复
热议问题