0

我有一个基类列表,List(Of Person)其中包含子类Employee和变量Manager

我想调用SetName列表中的每个人,但 vb.net 没有选择最窄的重载SetName函数。有没有办法自动选择最窄的SetName函数?这是我的示例代码

Module Example
    Class Person
        Public Name As String
    End Class

    Class Employee
        Inherits Person
        Public EmployeeID As String
    End Class

    Class Manager
        Inherits Person
        Public ManagerID As String
    End Class

    Sub Main()
        Dim PersonList As New List(Of Person)
        Call PersonList.Add(New Employee)
        Call PersonList.Add(New Manager)

        For Each CurrentPerson As Person In PersonList
            Call SetName(CurrentPerson)
        Next
    End Sub

    Public Sub SetName(AnyManager As Manager)
        AnyManager.Name = "Manager Tom"
    End Sub

    Public Sub SetName(AnyEmployee As Employee)
        AnyEmployee.Name = "Employee Bob"
    End Sub

    Public Sub SetName(AnyPerson As Person)
        AnyPerson.Name = "Person Jim"
    End Sub
End Module

列表中的每个人都以“Person Jim”结尾,但我希望第一个Employee是“员工 Bob”,第二个Manager是“经理 Tom”

这是最干净的方法吗?这是我能想到的唯一方法。在我的情况下这会变得混乱,因为我有大约 20 个派生类而不是 2 个。

For Each CurrentPerson As Person In PersonList
    If TypeOf (CurrentPerson) Is Employee Then
        Call SetName(CType(CurrentPerson, Employee))
    ElseIf TypeOf (CurrentPerson) Is Manager Then
        Call SetName(CType(CurrentPerson, Manager))
    End If
Next

编辑:我想我没有让我的需求更清楚。Setname 只是为了代表任何需要列表中的窄类型而不是基类的函数。实际名称并不重要。也许我应该使用一个函数实际上与员工 ID 和经理 ID 混淆的示例。抱歉没有更清楚。

4

1 回答 1

0

不知道你在找什么,但下面的代码会得到你想要的名字。我摆脱了 SetName subs 并在类的构造函数中设置了 name 属性。您可以创建一个参数化的构造函数来设置不同的名称。顺便说一句,在大多数情况下不需要调用语句。

Private Sub OPCode()
    Dim PersonList As New List(Of Person)
    PersonList.Add(New Employee)
    PersonList.Add(New Manager)

    For Each CurrentPerson In PersonList
        Debug.Print(CurrentPerson.Name)
    Next
End Sub

Class Employee
    Inherits Person
    Public EmployeeID As String
    Public Sub New()
        Name = "Employee Bob"
    End Sub
End Class

Class Manager
    Inherits Person
    Public ManagerID As String
    Public Sub New()
        Name = "Manager Tom"
    End Sub
End Class

Class Person
    Public Name As String
    Public Sub New()
        Name = "Person Jim"
    End Sub
End Class

使用您的 SetName subs 和没有参数化构造函数的代码...

Private Sub OPCode()
    Dim PersonList As New List(Of Person)
    Dim e As New Employee()
    SetName(e)
    PersonList.Add(e)
    Dim m As New Manager()
    SetName(m)
    PersonList.Add(m)

    For Each CurrentPerson In PersonList
        Debug.Print(CurrentPerson.Name)
    Next
End Sub

Public Sub SetName(AnyManager As Manager)
    AnyManager.Name = "Manager Tom"
End Sub

Public Sub SetName(AnyEmployee As Employee)
    AnyEmployee.Name = "Employee Bob"
End Sub

Public Sub SetName(AnyPerson As Person)
    AnyPerson.Name = "Person Jim"
End Sub

Class Employee
    Inherits Person
    Public EmployeeID As String
End Class

Class Manager
    Inherits Person
    Public ManagerID As String
End Class

Class Person
    Public Name As String
End Class

两个代码都产生...

Employee Bob
Manager Tom

在即时窗口中

于 2019-11-16T00:17:03.077 回答