这似乎是一个合理的(也许是简单的?)场景,但您将如何执行以下操作:
假设我有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属性?
以下是我想知道的情况:我正在使用psuedo MVC arhitecture编写UserControl,其中UserControl将扩展接口公开给它的Controller,并将Simple接口公开给控件的使用者。
顺便说一下,在VB.NET中实现它(vb中任何建议的synatx都会受到赞赏)。
答案 0 :(得分:6)
很抱歉,但我不掌握VB.Net语法。
在C#中,您可以隐式或显式地实现接口。 如果您的类Foo将ErrorMsg实现为公共方法,则此实现将用于两个接口。
如果您想要不同的实现,可以明确地实现它:
string ISimpleInterface.ErrorMsg {get { ... } }
string IExtendedInterface.ErrorMsg {get { ... } set { ... }}
答案 1 :(得分:5)
您可以使用“显式接口”实现实现其中一个或两个接口,因此编译器知道哪个ErrorMsg属性属于哪个接口。
为此,只需在类名后面写:ISimpleInterface(对于C#),然后单击ISimpleInterface并选择实现显式接口。
此处提供更多信息:http://msdn.microsoft.com/en-us/library/aa288461(VS.71).aspx
答案 2 :(得分:3)
在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
}
答案 3 :(得分:2)
在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
答案 4 :(得分:2)
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
答案 5 :(得分:0)
您需要使用显式接口实现。有关此主题的更多信息,请访问:http://msdn.microsoft.com/en-us/library/aa288461(VS.71).aspx
答案 6 :(得分:0)
您可以让私有子例程实现接口。仅在将对象分配给该接口类型的变量时才会公开它。
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
答案 7 :(得分:0)
虽然显式实现将解决这个问题,如其他人所示,在这种情况下我可能会让IExtendedInterface实现ISimpleInterface(ErrorMsg属性在语义上是相同的属性,它意味着在我猜的这两个接口中相同)。
interface ISimpleInterface
{
string ErrorMessage { get; set; }
}
interface IExtendedInterface : ISimpleInterface
{
string SomeOtherProperty { get; set; }
}