简体   繁体   English

子接口的 Load(x) 函数隐藏父接口的 Load() 函数

[英]Child Interface's Load(x) function hides parent interface's Load() function

I have the following generic interface我有以下通用接口

Public Interface IGenericDAO(Of T As {Class, IEntity})

    Sub Save(entity As T)

    Sub Delete(entity As T)

    Sub Delete(id As Long)

    Function Load(id As Long) As T

    Function Load() As List(Of T)

End Interface

And I'd like to have another one like this我想要另一个这样的

Public Interface IAgendaDAO
    Inherits IGenericDAO(Of Agenda)

    Function Load(isPublished As Boolean, meetingType As MeetingType) As List(Of Agenda)

    Function Load(meetingType As Boolean) As List(Of Agenda)

End Interface

But in this case Compiler says that my Load functions hides my Load function without arguments I can do such thing in C#, Why I can't in VB.NET?但在这种情况下,编译器说我的 Load 函数隐藏了我的 Load 函数而没有参数我可以在 C# 中做这样的事情,为什么我不能在 VB.NET 中? What can I do to fix that?我能做些什么来解决这个问题? Should I name my functions each differently?我应该为我的函数分别命名吗?

Yeah, this is one of the basic choices that a language designer needs to make.是的,这是语言设计师需要做出的基本选择之一。 A core decision is what exactly should happen when a derived class has a member with the exact same name as a member in the base class.核心决策是当派生类具有与基类中的成员同名的成员时究竟应该发生什么。 This can hide the inherited base class member and make it difficult to use the base member.这会隐藏继承的基类成员,并使基类成员难以使用。 And it is risky , a innocent maintenance change might suddenly no longer hide the base member and break existing code.而且这是有风险的,一个无辜的维护更改可能会突然不再隐藏基本成员并破坏现有代码。

The C# language designers went for "hide-by-signature". C# 语言设计者选择了“隐藏签名”。 In other words, a method only hides if the base method has the exact same name and the exact same arguments.换句话说,只有当基方法具有完全相同的名称完全相同的参数时,方法才会隐藏。 This is what you are used to and hoped to happen in these interface declarations.这是您习惯并希望在这些接口声明中发生的事情。

While other languages, like C++ and VB.NET went for "hide-by-name".而其他语言,如 C++ 和 VB.NET,则采用“隐藏名称”。 In other words, a method hides when it has the same name, regardless of what arguments it takes.换句话说,一个方法在具有相同名称时隐藏,而不管它采用什么参数。 It is a "less surprises" choice, it is much easier to reason through and maintenance changes are less likely to alter behavior of existing code.这是一个“较少惊喜”的选择,它更容易推理并且维护更改不太可能改变现有代码的行为。

You can see this at work in this sample code:您可以在此示例代码中看到这一点:

Class Base
    Sub foo()
    End Sub
End Class

Class Derived
    Inherits Base

    Sub foo(i As Integer)   '' Warning: 'foo' shadows an overloadable member declared in Base
    End Sub

    Shadows Sub foo(d As Double)   '' No warning, Shadows suppresses it
    End Sub
End Class

Sub Main()
    Dim obj = New Derived
    obj.foo()          '' Error!
    Dim base As Base = obj
    base.foo()         '' Okay
End Sub

Do note that this is just a warning, it is not an error.请注意,这只是一个警告,而不是错误。 It just foreshadows the potential pain you will inflict on the client code programmer.它只是预示了您将给客户端代码程序员带来的潜在痛苦。 You can hide the warning easily, put the Shadows keyword in front of the Load() method.您可以轻松隐藏警告,将Shadows关键字放在 Load() 方法之前。 But certainly consider avoiding the pain and suffering in the client code and just pick another name so there is never an ambiguity.但当然要考虑避免客户端代码中的痛苦和痛苦,只需选择另一个名称,这样就不会产生歧义。

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

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