实现 2 个具有“相同名称”的接口 特性

发布于 2024-07-13 14:31:31 字数 609 浏览 4 评论 0原文

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

假设我有 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 向其控制器公开扩展接口,并向控件的使用者公开 Simple 接口。

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

This seems like a reasonable (and maybe simple?) scenario, but how would you do the following:

Lets say I have 2 interfaces:

Interface ISimpleInterface
    string ErrorMsg { get; } 
End Interface

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

I want a class to implement both interfaces:

Public Class Foo Implements ISimpleInterface, IExtendedInterface

How do I define the ErrorMsg property in the class given that each interface has a different access level?

Here is my scenario in case you are wondering: I am writing a UserControl using a psuedo MVC arhitecture, where the UserControl exposes the extended interface to it's Controller, and exposes the Simple interface to the Consumers of the control.

By the way, implementing this in VB.NET (any suggested synatx in vb would be appreciated).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

花伊自在美 2024-07-20 14:31:31

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

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

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

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

Sorry but I don't master VB.Net syntax.

In C# you can implement interfaces implicitly or explicitly.
If your class Foo implements ErrorMsg as a public method, this implementation will be used for both interface.

If you want distinct implementation you can implement it explicitly :

string ISimpleInterface.ErrorMsg {get { ... } } 
string IExtendedInterface.ErrorMsg {get { ... } set { ... }} 
如何视而不见 2024-07-20 14:31:31

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

为此,只需在类名后写入 :ISimpleInterface(对于 C#),然后单击 ISimpleInterface 并选择实现显式接口。

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

You can implement one of them or both interfaces with an 'explicit interface' implementation, so the compiler knows which ErrorMsg property belongs to which interface.

To do this simply write :ISimpleInterface (for C#) after your class name and then click on ISimpleInterface and select implement explicit interface.

More information here: http://msdn.microsoft.com/en-us/library/aa288461(VS.71).aspx

客…行舟 2024-07-20 14:31:31

在 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
}

In C#, an implicit implementation (with the set) can satisfy both of these:

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

If you want to change it, you can use explicit implementation ("Implements" in VB?) - in C#:

string ISimpleInterface.ErrorMsg
{
    get { return ErrorMsg; } // or something more interesting
}
慈悲佛祖 2024-07-20 14:31:31

在 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

In C# you can use explicit interface implementation:

class Foo
{
    string ISimpleInterface.ErrorMsg
    { get... }

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

    string IExtendedInterface.SomeOtherProperty
    { get... set... }
}

or Interface Mapping

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

    public string SomeOtherProperty
    { get... set... }
}

As for VB.NET, it has Implements keyword:

Property ErrorMsg As String Implements ISimpleInterface.ErrorMsg

Property OtherErrorMsg As String Implements IExtendedInterface.ErrorMsg
橘和柠 2024-07-20 14:31:31

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

The Implements keyword in VB.NET makes this easy:

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
耳钉梦 2024-07-20 14:31:31

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

You will need to work with Explicit Interface Implementations. More on the subject here: http://msdn.microsoft.com/en-us/library/aa288461(VS.71).aspx

青春有你 2024-07-20 14:31:31

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

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

While explicit implementation will solve this, as shown by others, in this very case i would probably let IExtendedInterface implement ISimpleInterface (ErrorMsg property is semantically the same property, it means the same in these 2 interfaces i would guess).

interface ISimpleInterface
{
    string ErrorMessage { get; set; }
}
interface IExtendedInterface : ISimpleInterface
{
    string SomeOtherProperty { get; set; }
}
月依秋水 2024-07-20 14:31:31

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

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

You can have private subroutines implement interfaces. It is only exposed when the object is assigned to a variable of that interface type.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文