9

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

假设我有 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 将不胜感激)。

4

8 回答 8

6

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

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

如果你想要不同的实现,你可以显式地实现它:

string ISimpleInterface.ErrorMsg {get { ... } } 
string IExtendedInterface.ErrorMsg {get { ... } set { ... }} 
于 2009-02-10T12:51:56.240 回答
5

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

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

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

于 2009-02-10T12:50:19.927 回答
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
}
于 2009-02-10T12:52:14.210 回答
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
于 2009-02-10T12:54:52.690 回答
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
于 2009-02-10T13:06:28.747 回答
0

您将需要使用显式接口实现。更多关于这里的主题:http: //msdn.microsoft.com/en-us/library/aa288461 (VS.71).aspx

于 2009-02-10T12:51:23.900 回答
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
于 2009-02-10T13:42:47.710 回答
0

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

interface ISimpleInterface
{
    string ErrorMessage { get; set; }
}
interface IExtendedInterface : ISimpleInterface
{
    string SomeOtherProperty { get; set; }
}
于 2009-02-10T13:54:08.617 回答