Confused with override and new functionality C#
This question already has an answer here:
virtual
it can be overrided in the sub classes. If you want to change a method (which was declared virtual in base class) in sub class you can use both new
and override
keywords but there is a difference between them
. When using new : if you cast an object of sub class as base class, then call that method, base class implementation will run
. When using override : if you cast an object of sub class as base class, then call that method, sub class implementation will run.
Here is the code
AbstractClass instance = new DerivedClass();
instance.SayGoodbye(); //See you later - In derived class I'm in NEW member
But if you use override
AbstractClass instance = new DerivedClass();
instance.SayGoodbye(); //Goodbye- abstract member n
链接地址: http://www.djcxy.com/p/38132.html
上一篇: 在继承的虚函数中调用子类的方法?
下一篇: 与重写和新功能混淆C#