小编Geo*_*pos的帖子

具有接口属性的 C# 接口实现

我开始更深入地研究 C# 编程,并且一直在研究接口。

我了解接口的基础知识,因为它们应该是实现它们的任何类的“契约”。接口中定义的任何内容都需要在继承(不确定这是否是正确的术语)它们的任何类中实现。

因此,我向接口添加了一个属性,该属性本身就是一个接口。当尝试实现父接口时,我向我的类添加了一个属性,该类实现了父接口中的属性接口。这个解释可能有点令人困惑,所以我在下面添加了一些代码。

interface IPropertyThatIsAnInterface
{
    public int X { get; set; }
}

class ClassThatImplementsIPropertyThatIsAnInterface : IPropertyThatIsAnInterface
{
    public int X { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
interface IAnInterface
{
    public IPropertyThatIsAnInterface InterfaceProperty { get; set; }
}

class ClassThatImplementsIAnInterface : IAnInterface
{
    public ClassThatImplementsIPropertyThatIsAnInterface InterfaceImplmentationProperty { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

Visual Studio(在 .Net Core 3.0 [即 C# 8.0] 中)出现错误,指出我的类ClassThatImplementsIAnInterface未实现接口属性 ,public IPropertyThatIsAnInterface InterfaceProperty { get; set; }(这是一个接口属性)。这是 C# 的限制还是我对接口理解的限制?

我这样做的原因是因为我想IPropertyThatIsAnInterface为ClassThatImplementsIAnInterface我创建的每个对象实现自定义。IE …

c# interface-implementation .net-core

5
推荐指数
1
解决办法
1万
查看次数

标签 统计

.net-core ×1

c# ×1

interface-implementation ×1