c# - 接口(interface)实现错误

标签 c#

我是 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/

相关文章:

c# - 检测SQL Server 2008 R2

c# - 更新 Access 数据库

c# - 带有继承的用户控件中的 Eval

c# - DataGrid.ItemsSource 更改时如何引发事件

c# - ASHX 图像处理程序适用于 Chrome,不适用于 IE8

c# - 如何在日期中保留 0

c# - 在 WCF 中使用 DataContract 传递对象

c# - WPF/C# Textwrapping in a scrollviewer 使窗口大小调整滞后

c# - 为什么我的错误没有被捕获?

c# - WCF REST 获取响应 504 json 数据太大