What are some good alternatives to multiple-inheritance in .NET?

后端 未结 6 1407
梦如初夏
梦如初夏 2020-12-13 13:49

I\'ve run into a bit of a problem with my class hierarchy, in a WPF application. It\'s one of those issues where you have two inheritance trees merging together, and you can

6条回答
  •  时光说笑
    2020-12-13 14:18

    You have two options here; use interfaces, or use composition. Honestly, interfaces are very powerful, and after reading this line

    The interface solution doesn't make any sense to me. (Interfaces have never really made much sense to me, to be honest...)

    I think that you should learn how to use them properly. That said, if there is simply some logic that multiple class need, but it does not make sense for these classes to inherit from the same base class, just create a class to encapsulate that logic and add a member variable of that class to your classes that are giving you problems. This way all of the classes contain the logic but can be separate in their inheritance hierarchies. If the classes should implement a common interface(s), then use interfaces.

提交回复
热议问题