加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 背后的原因是什么不允许派生类的具体属性实现接口的接口属

发布时间:2020-12-16 01:50:16 所属栏目:百科 来源:网络整理
导读:这是一个非常简单的例子. public interface IMyInterfaceProperty{}public class MyProperty : IMyInterfaceProperty{}public interface IMyInterface{ IMyInterfaceProperty SomeProperty { get; set; }}public class MyClass : IMyInterface{ public MyPro
这是一个非常简单的例子.

public interface IMyInterfaceProperty
{
}

public class MyProperty : IMyInterfaceProperty
{
}

public interface IMyInterface
{
    IMyInterfaceProperty SomeProperty { get; set; }
}

public class MyClass : IMyInterface
{
    public MyProperty SomeProperty { get; set; }
}

在此示例中,MyProperty派生自IMyInterfaceProperty,但不允许.不允许编译的思考过程是什么?

Program.MyClass没有实现接口成员Program.IMyInterface.SomeProperty. Program.MyClass.SomeProperty无法实现Program.IMyInterface.SomeProperty,因为它没有匹配的返回类型Program.IMyInterfaceProperty.

解决方法

即使以下情况也是不允许的:

public interface IMyInterface
{
    IMyInterfaceProperty SomeProperty { get; set; }
    MyProperty SomeProperty { get; set; }
}

我认为原因是该类型中不允许具有相同签名的成员.这里不考虑返回值:

The signature of a method specifically does not include the return
type,nor does it include the params modifier that may be specified
for the right-most parameter.

(从http://msdn.microsoft.com/en-us/library/aa691131(v=vs.71).aspx开始.虽然是VS2003,但我认为自那时以来没有改变:))

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读