我怎么能叫一个重写的虚方法的“基本实现”? [英] How can I call the 'base implementation' of an overridden virtual method?
本文介绍了我怎么能叫一个重写的虚方法的“基本实现”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
,是有办法,我可以调用A类的版本方法的X?
A级
{
虚拟无效X(){Console.WriteLine(×); }
}B类:一
{
覆盖空隙X(){Console.WriteLine(Y); }
}类节目
{
静态无效的主要()
{
A B =新的B();
//调用A.X不知何故,没有B.X ...
}
解决方案
您不能。如果你真的需要做的,再有就是在设计中的一个缺陷 - 即该函数不应该是虚拟的,首先,还是基本函数的部分应被提取到一个单独的非虚拟函数
您可以从里面B.X但是打电话A.X
B类:一
{
覆盖无效X(){
base.X();
Console.WriteLine(Y);
}
}
但是,这是别的东西。
Given the following code, is there a way I can call class A's version of method X?
class A
{
virtual void X() { Console.WriteLine("x"); }
}
class B : A
{
override void X() { Console.WriteLine("y"); }
}
class Program
{
static void Main()
{
A b = new B();
// Call A.X somehow, not B.X...
}
解决方案
You cannot. If you really need to do that, then there is a flaw in your design - i.e. that function shouldn't be virtual to begin with, or part of the base function should be extracted to a separate non-virtual function.
You can from inside B.X however call A.X
class B : A
{
override void X() {
base.X();
Console.WriteLine("y");
}
}
But that's something else
这篇关于我怎么能叫一个重写的虚方法的“基本实现”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文