【问题标题】:Vb conceptual understanding of creating objects within a classvb在类内创建对象的概念理解
【发布时间】:2014-02-28 17:29:30
【问题描述】:

我想知道您是否可以帮助我了解如何创建一个 Card 对象(具有价值和花色)并使用其中的 52 张牌来制作一个名为 Deck 的对象。

我已经创建了我的卡片类,如何初始化卡片组类中的每张卡片?我应该一件一件地做吗?如何将所有这些卡链接到一副牌。

谢谢

【问题讨论】:

    标签: vb.net


    【解决方案1】:

    碰巧我今天早些时候确实阅读了您之前的问题。

    首先,创建一个西装枚举。

    Public Enum Suit As Integer
        Hearts = 1
        Diamonds = 2
        Clovers = 3
        Spades = 4
    End Enum
    

    然后创建卡片类。请注意,属性是只读的,因为卡片永远不会更改其值。 (如果你是魔术师,也许不是真的)

    Public Class Card
    
        Public Sub New(suit As Suit, value As Integer)
            Me.m_suit = suit
            Me.m_value = value
        End Sub
    
        Public ReadOnly Property Suit() As Suit
            Get
                Return Me.m_suit
            End Get
        End Property
    
        Public ReadOnly Property Value() As Integer
            Get
                Return Me.m_value
            End Get
        End Property
    
        Private m_suit As Suit
        Private m_value As Integer
    
    End Class
    

    最后,创建卡片组并填充 52 张卡片。

    Public Class Deck
    
        Public Sub New()
            Dim cards = New Card(52 - 1) {}
            Dim num As Integer = 0
            For s As Integer = 1 To 4
                For v As Integer = 1 To 13
                    cards(num) = New Card(CType(s, Suit), v)
                    num += 1
                Next
            Next
            Me.m_cards = New Collections.ObjectModel.ReadOnlyCollection(Of Card)(cards)
        End Sub
    
        Public ReadOnly Property Cards() As Collections.ObjectModel.ReadOnlyCollection(Of Card)
            Get
                Return Me.m_cards
            End Get
        End Property
    
        Private ReadOnly m_cards As Collections.ObjectModel.ReadOnlyCollection(Of Card)
    
    End Class
    

    【讨论】:

    • 非常感谢,你太棒了
    • 问题,我是 VB 的新手,你在哪里创建你的枚举部分是在类中还是在主程序中?
    • @user3363744 对于每个项目(枚举、类、.. 或其他)只需右键单击解决方案窗格中的项目,转到Add > New,选择Class并输入项目名称。例如。 Suit.vb。然后把VS创建的代码去掉,换成Public Enum Suit As Integer等等。
    【解决方案2】:

    你需要两个枚举和两个类:

    • 枚举
      1. CardFaceValue - 值范围为 Ace-10(含)、Jack、Queen、King。
      2. CardFaceType - 具有红心、黑桃、梅花、方块等值
      1. Deck - 有一个属性来包含所有卡片的集合
        1. 卡片 - 卡片类型数组,大小为 52。
      2. 卡 - 有两个属性
        1. 卡面值
        2. 卡面类型

    在 Deck 类的构造函数中,在一个循环中运行一个循环。对于 CardFaceType 枚举,外循环将运行 4 次,对于卡片 1-10、J、Q、K,内循环将运行 13 次。

    通过这些循环遍历枚举值并将卡片添加到您的牌组。

    【讨论】:

      【解决方案3】:

      这只是我设想的草稿

      你首先需要卡片类。

      Public Class Card
      
          Private cSuit As String
          Private cValue As Integer
      
          Public Property suit() As String
              Get
                  Return cSuit
              End Get
              Set(ByVal value As String)
                  cSuit = value
              End Set
          End Property
      
          Public Property value() As Integer
              Get
                  Return cValue
              End Get
              Set(ByVal value As Integer)
                  value = cValue
              End Set
          End Property
      
          Public Sub New(ByVal TheSuit As String, ByVal TheValue As Integer)
      
              cSuit = TheSuit
              cValue = TheValue
      
          End Sub
      

      然后您可以为每张卡片制作一个新对象并将其添加到卡片组中。

      Dim Deck As New List(Of Card)
              Dim Suit As String = "Spade"
              Dim Value As Integer = 11
      
              Dim AceOfSpades As New Card(Suit, Value)
      
              Deck.Add(AceOfSpades)
      

      【讨论】:

      • 但是牌组可能不完整,有重复和/或超过 52 张牌。
      • 如果您想要一副不完整的牌或一副以上的牌来增加几率怎么办?许多游戏使用不止一副牌。我认为一个模糊问题的基本适应性解决方案是公平的。更进一步,如果他没有使用标准套牌怎么办。如果是 Yugio 或 Pokemon 什么的。
      • 如果 OP 只是想要一个集合,为什么他会专门要求一个 Deck 类?他还特别要求将卡片链接到甲板上。在您的代码中,该卡很可能位于多个套牌中。
      • 如果你有一个收藏,那就只有一副。
      • 这是我采用的方法。问题,您是否还需要添加一个方法 .Add?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多