简体   繁体   English

使用重载的成员vb.net实现继承的接口

[英]Implementing Inherited Interface with Overloaded Member vb.net

I am trying to implement a derived interface in a class. 我试图在类中实现派生接口。 My interfaces and class are similar to the following. 我的接口和类与以下类似。 The Namespaces relate to different projects that hold these interfaces and the class: 命名空间与包含这些接口和类的不同项目相关:

Namespace ns1

    Public Interface IParent

        Function myFunction() As Double

    End Interface

End ns1

Namespace ns2

    Public Interface IDerived
        Inherits ns1.IParent

        Overloads / Shadows Function myFunction(ByRef myObject as Object) As Double

    End Interface

End ns2

Namespace ns3

    Public Class myClass
        Implements ns2.IDerived

        Public Function myFunction(ByRef obj as Object) As Double Implements ns2.IDerived.myFunction

        End Function

End ns3

In the derived interface, I am trying to overload the function in a way that when I implement the derived interface, I only have to implement the function as defined therein - as is done with the code above on "myClass". 在派生接口中,我试图以一种方式重载函数,当我实现派生接口时,我只需要实现其中定义的函数 - 就像在“myClass”上面的代码一样。 However, I am getting an error saying I have to also implement the function from the parent interface (with the empty argument list). 但是,我收到一个错误,说我还必须从父接口(使用空参数列表)实现该函数。 The error exists regardless of my using Overloads or Shadows on the function in the derived interface - both cause the error. 无论我在派生接口中使用函数的Overloads还是Shadows,都会出现错误 - 两者都会导致错误。

Is there anyway to accomplish what I am trying to do - implement only the derived interface's function in my class - using interfaces? 反正有没有完成我想要做的事情 - 只在我的类中实现派生接口的功能 - 使用接口? If there is not a way using interfaces, can anyone suggest an alternate way? 如果没有办法使用接口,有人可以提出另一种方式吗? We really need to use interfaces and are trying to avoid using classes. 我们确实需要使用接口并试图避免使用类。 That said, abstract classes my allow us to do all we need to do with these. 也就是说,抽象类我允许我们做所有我们需要做的事情。

I have read a lot of info on all the topics covered by this question as every concept is pretty basic and well covered in online help. 我已经阅读了关于这个问题涵盖的所有主题的大量信息,因为每个概念都非常基本,并且在在线帮助中有很好的介绍。 But, I have not found anything that I recognize as a direct solution to my specific issue. 但是,我没有找到任何我认为可以直接解决我具体问题的方法。

Thanks in advance for any help. 在此先感谢您的帮助。

I don't believe that what you want to accomplish is possible the way you are trying... As I recall when you inherit an Interface any class that implements your derived Interface is actually being told that it must implement both Interfaces rather allowing the options you have in a full class. 我不相信你想要完成的是你可能尝试的方式...我记得当你继承一个接口时,任何实现派生接口的类实际上被告知它必须实现两个接口而不是允许选项你有一个完整的课程。

So effectively what you have in myClass is: 所以你在myClass中有效的是:

Namespace ns3

    Public Class myClass
        Implements ns2.IDerived
        Implements ns1.IParent

        Public Function myFunction(ByRef obj as Object) As Double Implements ns2.IDerived.myFunction

        End Function

End ns3

So inheriting an interface is really just a way to enforce that a class implementing the derived interface must also implement the base interface. 因此,继承接口实际上只是强制实现派生接口的类也必须实现基接口的一种方法。

I don't know if this is a typo but you have two distinct methods: one that takes no parameter, and another that takes an object, so the compiler requirement is legitimate. 我不知道这是否是一个拼写错误,但你有两个不同的方法:一个不带参数,另一个带一个对象,所以编译器要求是合法的。

If this is a typo and that you have only one method, say " myFunction() ", then I fear VB.Net does not act like C# by simply hiding the base interface and allowing to only implement the derived one. 如果这是一个拼写错误并且您只有一个方法,比如说“ myFunction() ”,那么我担心VB.Net不会像C#那样只是隐藏基本接口而只允许实现派生的接口。

But you could easily fix this by forwarding: 但您可以通过转发轻松解决此问题:

Namespace ns1

    Public Interface IParent

        Function myFunction() As Double

    End Interface

End Namespace

Namespace ns2

    Public Interface IDerived
        Inherits ns1.IParent

        Function myFunction() As Double

    End Interface

End Namespace

Namespace ns3

    Public Class Class1
        Implements ns2.IDerived

        Public Function myFunction() As Double Implements ns2.IDerived.myFunction
            Return 42
        End Function

        Private Function myFunction1() As Double Implements ns1.IParent.myFunction
            Return myFunction()
        End Function
    End Class

End Namespace

Module Module1

    Sub Main()
        Dim cp As ns1.IParent = New ns3.Class1
        cp.myFunction()

        Dim cd As ns2.IDerived = New ns3.Class1
        cd.myFunction()
    End Sub

End Module

EDIT: So was not a typo, here is the standard (good/best practice?) fix: 编辑:所以不是一个错字,这是标准(良好/最佳实践?)修复:

Public Class Class1
    Implements ns2.IDerived

    Public Function myFunction(ByRef obj As Object) As Double Implements ns2.IDerived.myFunction

    End Function

    Public Function myFunction() As Double Implements ns1.IParent.myFunction
        Throw New NotImplementedException("'ns1.IParent.myFunction' has not been implemented because unicorns can't fly!")
    End Function
End Class

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM