Convert derived class to base class

后端 未结 6 1309
渐次进展
渐次进展 2020-12-01 13:31

I\'m trying to refresh my memory but can\'t find answers with Google.

public class BaseClass
{
    public virtual void DoSomething()
    {
        Trace.Writ         


        
6条回答
  •  一整个雨季
    2020-12-01 14:27

    You can't - that's entirely deliberate, as that's what polymorphism is all about. Suppose you have a derived class which enforces certain preconditions on the arguments you pass to an overridden method, in order to maintain integrity... you don't want to be able to bypass that validation and corrupt its internal integrity.

    Within the class itself you can non-virtually call base.AnyMethod() (whether that's the method you're overriding or not) but that's okay because that's the class itself deciding to potentially allow its integrity to be violated - presumably it knows what it's doing.

提交回复
热议问题