根据其他值将属性声明为不同的类

发布于 2025-01-11 08:32:49 字数 541 浏览 0 评论 0原文

我有:ClassUser、ClassClient、ClassPerson。

ClassUsser

  • 公共属性名称为 String

ClassClient

  • 公共属性名称为 String

ClassPerson

  • 公共属性 KindPerson 作为整数
  • 公共属性人为(ClassClient 或 ClassPerson)

我希望 delare Property Person 作为基于值的不同类型的类KindPerson。

Enum KindsPerson { 用户=1, Client=2}

首先,我会设置 KindPerson 的值,然后通过 set/get 方法获取 Person

我不知道在 VB 中是否可行。网。有什么办法可以做到这一点吗?

谢谢

I have: ClassUsser, ClassClient, ClassPerson.

ClassUsser

  • Public Property Name as String

ClassClient

  • Public Property Name as String

ClassPerson

  • Public Property KindPerson as Integer
  • Public Property Person as (ClassClient or ClassPerson)

I want delare Property Person as diferent type of class based on value of KindPerson.

Enum KindsPerson
{ User=1,
Client=2}

First, I would set value of KindPerson, and after, through set/get methods, obtain the Person

I don't Know If It si posible in VB.Net. It exists any way to do this?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧人 2025-01-18 08:32:49

您可以使用工厂模式:

Module Module1

  Sub Main()
    Dim p1 = Person.GetPerson(Person.PersonKind_ENUM.Client, "Adam")
    Dim p2 = Person.GetPerson(Person.PersonKind_ENUM.User, "Barbara")
    Dim personList As New List(Of Person)
    personList.Add(p1)
    personList.Add(p2)

    For Each p In personList
      Console.WriteLine($"{p.GetType.Name}: {p.name}")
    Next
    Console.ReadKey()

  End Sub

  Public Class User
    Inherits Person

  End Class

  Public Class Client
    Inherits Person

  End Class

  Public MustInherit Class Person
    Property Name As String
    Public Enum PersonKind_ENUM
      User
      Client
    End Enum
    Public Shared Function GetPerson(personKind As PersonKind_ENUM, name As String) As Person
      Select Case personKind
        Case PersonKind_ENUM.Client
          Return New Client() With {.Name = name}
        Case PersonKind_ENUM.User
          Return New User() With {.Name = name}
        Case Else
          Throw New InvalidOperationException
      End Select
    End Function
  End Class
End Module

You could use the Factory Pattern:

Module Module1

  Sub Main()
    Dim p1 = Person.GetPerson(Person.PersonKind_ENUM.Client, "Adam")
    Dim p2 = Person.GetPerson(Person.PersonKind_ENUM.User, "Barbara")
    Dim personList As New List(Of Person)
    personList.Add(p1)
    personList.Add(p2)

    For Each p In personList
      Console.WriteLine(
quot;{p.GetType.Name}: {p.name}")
    Next
    Console.ReadKey()

  End Sub

  Public Class User
    Inherits Person

  End Class

  Public Class Client
    Inherits Person

  End Class

  Public MustInherit Class Person
    Property Name As String
    Public Enum PersonKind_ENUM
      User
      Client
    End Enum
    Public Shared Function GetPerson(personKind As PersonKind_ENUM, name As String) As Person
      Select Case personKind
        Case PersonKind_ENUM.Client
          Return New Client() With {.Name = name}
        Case PersonKind_ENUM.User
          Return New User() With {.Name = name}
        Case Else
          Throw New InvalidOperationException
      End Select
    End Function
  End Class
End Module
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文