在C#中实现多态性,如何做到最好? [英] implementing polymorphism in c#, how best to do it?
问题描述
这里的第一个问题,希望大家都能对我轻描淡写!
first question here, so hopefully you'll all go gently on me!
过去几天,我一直在阅读有关多态性的大量文章,并尝试将其应用于我在c#中所做的事情,并且似乎有几种不同的方法来实现它.我希望我能解决这个问题,但是即使我没有澄清,我也会很高兴.
I've been reading an awful lot over the past few days about polymorphism, and trying to apply it to what I do in c#, and it seems there are a few different ways to implement it. I hope I've gotten a handle on this, but I'd be delighted even if I haven't for clarification.
据我所知,我有3种选择:
From what I can see, I've got 3 options:
- 我可以从一个基础继承
类并使用关键字
我使用的任何方法上的"
virtual
" 希望我的派生类 覆盖. - 我可以使用虚拟方法实现抽象类 并这样做,
- 我可以使用界面吗?
- I can just inherit from a base
class and use the keyword
'
virtual
' on any methods that I want my derived classes to override. - I could implement an abstract class with virtual methods and do it that way,
- I could use an interface?
据我所见,如果我不需要任何基础实现逻辑,那么接口可以为我提供最大的灵活性(因为这样我就不会在多重继承等方面限制自己了),但是如果我要求基础能够在派生类正在执行的操作之上做某事,那么采用1或2会是更好的解决方案?
From what I can see, if I don't require any implementation logic in the base, then an interface gives me the most flexibility (as I'm then not limiting myself with regards multiple inheritance etc.), but if I require the base to be able to do something on top of whatever the derived classes are doing, then going with either 1 or 2 would be the better solution?
感谢大家的投入-这个周末,无论是在本网站还是在其他地方,我都读了很多东西,我认为我现在已经了解了这些方法,但我只想在语言正确的方式(如果我走对头的话).希望我也已正确标记了此标记.
Thanks for any input on this guys - I have read so much this weekend, both on this site and elsewhere, and I think I understand the approaches now, yet I just want to clarify in a language specific way if I'm on the right track. Hopefully also I've tagged this correctly.
干杯, 特里
推荐答案
接口提供了最多的抽象;您不受任何特定实现的约束(如果实现因其他原因必须具有不同的基类,则很有用).
An interface offers the most abstraction; you aren't tied to any specific implementation (useful if the implementation must, for other reasons, have a different base class).
对于真正的多态性,virtual
是必须的;多态性最常与类型子类关联...
For true polymorphism, virtual
is a must; polymorphism is most commonly associated with type subclassing...
您当然可以混合两者:
public interface IFoo {
void Bar();
}
class Foo : IFoo {
public virtual void Bar() {...}
}
class Foo2 : Foo {
public override ...
}
abstract
是另一回事; abstract
的选择确实是:基类是否可以明智地定义它?如果没有默认实现,则必须为abstract
.
abstract
is a separate matter; the choice of abstract
is really: can it be sensibly defined by the base-class? If there is there no default implementation, it must be abstract
.
当有许多常见的实现细节时,一个通用的基类可能会有用,并且仅通过接口进行复制将毫无意义;但有趣的是-如果实现在每个实现中都不会改变,则扩展方法提供了一种将其公开在interface
上的有用方法(这样,每个实现都不必这样做):
A common base-class can be useful when there is a lot of implementation details that are common, and it would be pointless to duplicate purely by interface; but interestingly - if the implementation will never vary per implementation, extension methods provide a useful way of exposing this on an interface
(so that each implementation doesn't have to do it):
public interface IFoo {
void Bar();
}
public static class FooExtensions {
// just a silly example...
public static bool TryBar(this IFoo foo) {
try {
foo.Bar();
return true;
} catch {
return false;
}
}
}
这篇关于在C#中实现多态性,如何做到最好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!