【问题标题】:vb.net: multiple inheritance in an interfacevb.net:接口中的多重继承
【发布时间】:2011-08-06 15:16:06
【问题描述】:

我在 VB.net 中遇到了关于多重继承的问题:

据我所知,VB.net 通常不支持多重继承,但您可以通过使用接口(使用“Implements”而不是“Inherits”)实现一种多重继承:

Public Class ClassName
    Implements BaseInterface1, BaseInterface2

End Class

这适用于类,但我希望有一个接口继承一些基本接口。类似的东西:

Public Interface InterfaceName
    Implements BaseInterface1, BaseInterface2

End Interface

但是接口不允许使用“Implements”关键字(当然,这是有道理的)。我尝试使用一种我从 Java 中知道的抽象类:

Public MustInherit Class InterfaceName
    Implements BaseInterface1, BaseInterface2

End Class

但现在我需要在 InterfaceName 类中实现 BaseInterface1 和 BaseInterface2 中定义的方法。但由于 InterfaceName 也应该是一个接口,我不想在该类中实现这些方法。

在 C# 中你可以很容易地做到这一点:

public interface InterfaceName: BaseInterface1, BaseInterface2 {}

你知道我是否可以在 VB.net 中做类似的事情吗?

【问题讨论】:

    标签: vb.net interface multiple-inheritance


    【解决方案1】:

    类似于 Java,在 VB.NET 接口中“扩展”其他接口。这意味着他们“继承”了他们的功能。他们没有实现它。

    Public Interface InterfaceName
        Inherits BaseInterface1, BaseInterface2
    End Interface
    

    【讨论】:

    • 你完全正确。我不知道为什么我使用了 Implements 关键字。当然你必须使用继承!坦克
    【解决方案2】:

    试试

    Public Interface InterfaceName
        Inherits BaseInterface1
        Inherits BaseInterface2
    End Interface
    

    【讨论】:

      【解决方案3】:

      一种解决方法是让抽象类(mustinherit)传递定义接口中的每个项目的工作,它不想用 mustoverride 实现。如果可能,请尝试在一般意义上预定义每一个,并使其可覆盖。

      【讨论】:

        【解决方案4】:

        继承接口时我会小心。

        虽然它有效,但我发现如果将 BindingList(Of InterfaceName) 绑定到 BindingSource 并将 BindingSource 绑定到 DataGridView,则 Visual Studio DataGridView 设计器看不到 Interface1 和 Interface2 中的属性,无法将其作为列分配给DataGridView。

        【讨论】:

        • 这是 IDE 的限制。它不应该对您的代码施加限制。
        猜你喜欢
        • 2011-10-05
        • 2017-03-24
        • 2013-02-07
        • 1970-01-01
        • 2014-12-31
        • 1970-01-01
        • 2014-02-18
        相关资源
        最近更新 更多