C#入门精典中的一个例子看不懂请帮助小弟!!
Card类: 程序代码:
public class Card { public readonly Suit suit; public readonly Rank rank; public override string ToString() { return "The " + rank + " of " + suit + "s"; } private Card() { } public Card(Suit newSuit, Rank newRank) { suit = newSuit; rank = newRank; } }
Deck类:
程序代码:
public class Deck { public Deck() { cards = new Card[52]; for (int suitVal = 0; suitVal < 4; suitVal++) { for (int rankVal = 1; rankVal < 14; rankVal++) { cards[suitVal * 13 + rankVal -1] = new Card((Suit)suitVal,(Rank)rankVal); } } } private Card[] cards; public Card GetCard(int cardNum) { if (cardNum >= 0 && cardNum <= 51) return cards[cardNum]; } }
控制台程序入口:
程序代码:
static void Main(string[] args) { Deck myDeck = new Deck(); myDeck.Shuffle(); for (int i = 0; i < 52; i++) { Card tempCard = myDeck.GetCard(i); Console.Write(tempCard.ToString()); if (i != 51) Console.Write(", "); else Console.WriteLine(); } }
我有两个问题:
一,我不明白其中Deck类的字段为什么可以这样定义private card[] cards?
二,为什么可以直接调用Card类中的重写的ToString方法?