使用“相同名称”属性实现2个接口

这似乎是一个合理的(也许是简单的?)场景,但您将如何执行以下操作:

可以说我有2个接口:

Interface ISimpleInterface string ErrorMsg { get; } End Interface Interface IExtendedInterface string ErrorMsg { get; set; } string SomeOtherProperty { get; set; } End Interface 

我想要一个类来实现这两个接口:

 Public Class Foo Implements ISimpleInterface, IExtendedInterface 

如果每个接口具有不同的访问级别,如何在类中定义ErrorMsg属性?

以下是我想知道的情况:我正在使用伪造的MVC架构编写UserControl,其中UserControl将扩展接口暴露给它的Controller,并将Simple接口暴露给控件的使用者。

顺便说一句,在VB.NET中实现这一点(vb中的任何建议的synatx将不胜感激)。

您可以使用“显式接口”实现实现其中一个或两个接口,因此编译器知道哪个ErrorMsg属性属于哪个接口。

要做到这一点,只需在类名后面写:ISimpleInterface(对于C#),然后单击ISimpleInterface并选择实现显式接口。

更多信息: http : //msdn.microsoft.com/en-us/library/aa288461(VS.71).aspx

抱歉,我不掌握VB.Net语法。

在C#中,您可以隐式或显式地实现接口。 如果您的类Foo将ErrorMsg实现为公共方法,则此实现将用于两个接口。

如果您需要不同的实现,可以显式实现它:

 string ISimpleInterface.ErrorMsg {get { ... } } string IExtendedInterface.ErrorMsg {get { ... } set { ... }} 

在C#中,隐式实现(使用set )可以满足以下两个条件:

 class Foo : ISimpleInterface, IExtendedInterface { public string ErrorMsg { get; set; } public string SomeOtherProperty {get; set;} } 

如果要更改它,可以使用显式实现(VB中的“Implements”) – 在C#中:

 string ISimpleInterface.ErrorMsg { get { return ErrorMsg; } // or something more interesting } 

在C#中,您可以使用显式接口实现:

 class Foo { string ISimpleInterface.ErrorMsg { get... } string IExtendedInterface.ErrorMsg { get... set... } string IExtendedInterface.SomeOtherProperty { get... set... } } 

或接口映射

 class Foo { public string ErrorMsg { get... set... } public string SomeOtherProperty { get... set... } } 

至于VB.NET,它有Implements关键字:

 Property ErrorMsg As String Implements ISimpleInterface.ErrorMsg Property OtherErrorMsg As String Implements IExtendedInterface.ErrorMsg 

VB.NET中的Implements关键字使这很容易:

 Public Interface ISimpleInterface ReadOnly Property ErrorMsg() As String End Interface Friend Interface IExtendedInterface Property ErrorMsg() As String Property SomeOtherProperty() As String End Interface Public Class Foo Implements ISimpleInterface, IExtendedInterface Private other As String Private msg As String Public Property ErrorMsgEx() As String Implements IExtendedInterface.ErrorMsg Get Return msg End Get Set(ByVal value As String) msg = value End Set End Property Public Property SomeOtherPropertyEx() As String Implements IExtendedInterface.SomeOtherProperty Get Return other End Get Set(ByVal value As String) other = value End Set End Property Public ReadOnly Property ErrorMsg() As String Implements ISimpleInterface.ErrorMsg Get Return msg End Get End Property End Class 

您将需要使用显式接口实现。 有关此主题的更多信息,请访问: http : //msdn.microsoft.com/en-us/library/aa288461(VS.71).aspx

虽然显式实现将解决这个问题,如其他人所示,在这种情况下,我可能会让IExtendedInterface实现ISimpleInterface(ErrorMsg属性在语义上是相同的属性,它意味着在我猜的这两个接口中相同)。

 interface ISimpleInterface { string ErrorMessage { get; set; } } interface IExtendedInterface : ISimpleInterface { string SomeOtherProperty { get; set; } } 

您可以让私有子例程实现接口。 仅当将对象分配给该接口类型的变量时才会公开它。

 Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim S As ISimpleInterface Dim Ext As IExtendedInterface Dim F As New Foo F.ErrorMsg = "Test Error" S = F Ext = F MsgBox(S.ErrorMsg) MsgBox(Ext.ErrorMsg) MsgBox(F.ErrorMsg) End Sub End Class Public Interface ISimpleInterface ReadOnly Property ErrorMsg() As String End Interface Public Interface IExtendedInterface Property ErrorMsg() As String Property SomeOtherProperty() As String End Interface Public Class Foo Implements ISimpleInterface, IExtendedInterface Private other As String Private msg As String Public Property ErrorMsg() As String Implements IExtendedInterface.ErrorMsg Get Return msg End Get Set(ByVal value As String) msg = value End Set End Property Public Property SomeOtherProperty() As String Implements IExtendedInterface.SomeOtherProperty Get Return other End Get Set(ByVal value As String) other = value End Set End Property Private ReadOnly Property ErrorMsgSimple() As String Implements ISimpleInterface.ErrorMsg Get Return ErrorMsg End Get End Property End Class