关于c#.net中的虚方法 [英] Regarding virtual method in c#.net
问题描述
我在受保护的覆盖中遇到错误int RowCount()
{}就像没有找到合适的方法来覆盖。
我尝试了什么:
我在sln中有两个项目
在X项目中 - > class是X1:方法就好。
protected override int RowCount()
{
逻辑......
返回计数;
}
在Y项目中 - > class是Y1
protected virtual int RowCount()
{
返回-1;
}
私有空格xx()
{
Count = RowCount(); < br $>
}
我也添加了参考文献,请帮我解决错误。
I am getting error at protected override int RowCount()
{ } like no suitable method found to override.
What I have tried:
I have two projects in sln
In X project --> class is X1:Method is like.
protected override int RowCount()
{
logic......
return Count;
}
In Y project --> class is Y1
protected virtual int RowCount()
{
return -1;
}
Private void xx()
{
Count = RowCount();
}
I have added references also,please help me how to avoid error.
推荐答案
您只能覆盖从原始派生的类中的方法:因此,除非X1是从Y1派生的,否则您无法覆盖它的方法。
You can only override methods in classes that are derived from the original: so unless X1 is derived from Y1 you cannot override it's methods.
public class X1 : Y1
{
protected override int RowCount()
{
return Count;
}
}
public class Y1
{
protected int Count = 666;
protected virtual int RowCount()
{
return -1;
}
}
可行。
这篇关于关于c#.net中的虚方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!