如何重用代码时,多重继承是不是一种选择? [英] How to reuse code when multiple inheritance is not an option?
问题描述
我想利用从几个我以前测试过班几个方法到我,我建立新类。不幸的是,C#不支持多重继承。如何从这些老班重用代码?难道我只是创建它们的成员对象?或者我是否有任何其他选项?
I would like to make use of few methods from couple of my old tested classes into my new class that I am building. Unfortunately, C# does not support multiple inheritance. How do I reuse code from these old classes? Do I just create them as member objects? or Do I have any other options?
推荐答案
一般情况下,使用成分,而不是继承的是前进的方向,是的。如果你能给什么样的你的意思是事情的一个具体的例子,这将使它更容易帮助,虽然找到合适的方法:它并不总是相同的
Generally, using composition instead of inheritance is the way forward, yes. If you could give a concrete example of the kind of thing you mean, that would make it easier to help find the appropriate approach though: it's not always the same.
这篇关于如何重用代码时,多重继承是不是一种选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!