【问题标题】:distributed interface implementation分布式接口实现
【发布时间】:2026-01-14 07:15:02
【问题描述】:

有没有办法在类层次结构中传播接口实现?考虑以下示例。

Public Interface I
    Property X As String
    Property Y As String
    Property Z As String
End Interface

Public Class A
    Property X As String
    Property Y As String
End Class

Public Class B
    Inherits A
    Implements I

    Property Z As String
End Class

我想避免在B 中重复XY。想想十个而不是两个。 而且我不想拆分接口I,因为B 的继承应该是实现细节。

【问题讨论】:

  • 使用 c#。如果用 c# 编写,上面的代码就可以正常工作。
  • 我担心。不幸的是,我无法将项目转换为 c#。

标签: vb.net interface class-hierarchy


【解决方案1】:

这可能不适用于您的情况,但您可以使用部分类。

这意味着你将失去 A 类 :-(

你在使用 MVC Razor HTML 帮助类吗?当接口没有在单个类中实现时,我认为他们不喜欢它。

Public Interface I
    Property X As String
    Property Y As String
    Property Z As String
End Interface


Partial Public Class B
    Property X As String Implements I.X
    Property Y As String Implements I.Y
End Class

Public Class B

    Implements I

    Property Z As String Implements I.Z
End Class

不知道如何解决这个问题...

编辑:

Public Interface iA
    Property X As String
    Property Y As String
End Interface

Public Interface iB
    Property Z As String
End Interface

Public Interface IAB
    Inherits iA
    Inherits iB
End Interface

Public Class A
    Implements iA
    Property X As String Implements iA.X
    Property Y As String Implements iA.Y
End Class

Public Class B
    Inherits A
    Implements IAB
    Property Z As String Implements iB.Z
End Class

【讨论】:

  • AB 已被使用。我想把大部分都封装在新的接口I中。
  • 正如我在问题中所说,我不想拆分界面。事实上,没有iB,你也可以做到这一点。