我是 C# 编程新手,我知道这是一个业余问题,所以请不要笑我!
我声明了这些接口(interface)
class derived : iInterface3
{
double[] d = new double[5];
public override int MyProperty
{
get
{
return 5;
}
set
{
throw new Exception();
}
}
int iProperty
{
get
{
return 5;
}
}
double this[int x]
{
set
{
d[x] = value;
}
}
}
class derived2 : derived
{
}
interface iInterface
{
int iProperty
{
get;
}
double this[int x]
{
set;
}
}
interface iInterface2 : iInterface
{ }
interface iInterface3 : iInterface2
{ }
即使我将 iInterface 的所有成员实现为派生类,但我仍然收到此错误。
'final_exam_1.derived' does not implement interface member 'final_exam_1.iInterface.this[int]'. 'final_exam_1.derived.this[int]' cannot implement an interface member because it is not public.
还有这个
'final_exam_1.derived' does not implement interface member 'final_exam_1.iInterface.iProperty'. 'final_exam_1.derived.iProperty' cannot implement an interface member because it is not public.
为什么?
提前感谢您的帮助!
最佳答案
您需要添加public
access modifier到从该类派生的所有成员。
作者:default他们的访问权限较低。
此外,您需要删除覆盖
,因为实现接口(interface)时没有任何内容可以覆盖。覆盖是指您希望覆盖一个虚拟方法。
class derived : iInterface3
{
double[] d = new double[5];
public int MyProperty
{
get
{
return 5;
}
set
{
throw new Exception();
}
}
public int iProperty
{
get
{
return 5;
}
}
public double this[int x]
{
set
{
d[x] = value;
}
}
}
您的代码还存在其他问题,但这些是无法编译的原因。
关于c# - 接口(interface)实现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14296328/