简体   繁体   English

Watson等人:Beginning Visual C#第10章练习5

[英]Watson et al: Beginning Visual C# Chapter 10 exercise 5

The question is for anyone who has referred to the solution provided for this question at the end of the book. 该问题适用于在本书结尾处提到此问题的解决方案的任何人。 I have set up Ch10CardLib as outlined at the end of the chapter. 我已经按照本章末尾所述设置了Ch10CardLib。 Running the included client console application displaying a shuffled deck is no problem. 运行附带的客户端控制台应用程序以显示随机播放的牌组没有问题。

I have run the solution provided to obtain an error message on the following line: 我已运行提供的解决方案以在以下行上获取错误消息:

Suit flushSuit = playDeck.GetCard(hand * 5).suit;

"Error 1 'Ch10CardLib.Card' does not contain a definition for 'suit' and no extension method 'suit' accepting a first argument of type 'Ch10CardLib.Card' could be found (are you missing a using directive or an assembly reference?)" “错误1'Ch10CardLib.Card'不包含'suit'的定义,并且找不到扩展方法'suit'接受类型为'Ch10CardLib.Card'的第一个参数(您是否缺少using指令或程序集引用? )”

"Suit" is an enum type, but "suit" is a private readonly field in the Card class. “ Suit”是枚举类型,但是“ suit”是Card类中的私有只读字段。

Can anyone divine the author's intention here? 有人能在这里说出作者的意图吗?

Just for "fun" I decided to include a method in the Card class: 为了“好玩”,我决定在Card类中包括一个方法:

public Suit Suit()
{
    return suit;
}

Suit flushSuit = playDeck.GetCard(hand * 5).Suit();

This produces the desired result, but is it in the spirit of the context? 这会产生预期的结果,但这是否符合上下文?

That's a syntax error. 那是语法错误。 If the code in the book doesn't compile (I don't have a copy of the book), then it's not your fault. 如果书中的代码没有编译(我没有这本书的副本),那不是你的错。 Your fix looks perfectly reasonable to me, although I would probably have exposed it as a readonly property rather than a method: 您的修复对我来说似乎完全合理,尽管我可能会将其公开为只读属性而不是方法:

public Suit Suit
{
    get { return suit; }
}

Perhaps you could swap the private readonly field for an auto property? 也许您可以将私有只读字段交换为自动属性?

public Suit Suit { get; private set; }

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

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