c# – 背后的原因是什么不允许派生类的具体属性实现接口的接口属
这是一个非常简单的例子.
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; } } 我认为原因是该类型中不允许具有相同签名的成员.这里不考虑返回值:
(从http://msdn.microsoft.com/en-us/library/aa691131(v=vs.71).aspx开始.虽然是VS2003,但我认为自那时以来没有改变:)) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |