使用Arrays实现纸牌游戏。 [英] Use Arrays to implement a card game.

查看:71
本文介绍了使用Arrays实现纸牌游戏。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友我正在阅读Microsoft Visual C#2013 Step by Step by John Sharp让我的基础知识清晰。



我在理解下面的代码时遇到了问题在书中说使用数组实现纸牌游戏。



1.选择两种枚举类型。

Hello Friends i am reading Microsoft Visual C# 2013 Step by Step by John Sharp to have my basics clear.

I have encountered a problem in understanding the following code in the book which says Use arrays to implement a card game.

1.Taking Two enumeration types.

enum Value { Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King,
Ace }




enum Suit { Clubs, Diamonds, Hearts, Spades }



2.创建PlayCard课程


2.Creating PlayingCard class

class PlayingCard
{
  private readonly Suit suit;
  private readonly Value value;
  
  public PlayingCard(Suit s, Value v)
  {
    this.suit = s;
    this.value = v;
  }
  /*public override string ToString()
  {
    
    return result;
  }*/
  
  public Suit CardSuit()
  {
    return this.suit;
  }
  
  public Value CardValue()
  {
    return this.value;
  }
}



在上面的课程中,我评论了部分代码,我无法理解它。



提前致谢。


In the above class i have commented part of the code which i am unable to understand it properly.

Thanks in advance.

推荐答案

评论部分不完整。

必须是:



The commented part is not complete.
It must be:

public override string ToString()
{
    string result = string.Format("{0} of {1}", this.value, this.suit);

    return result;
}





你错过了第一行。



方法ToString()用于替换每个对象具有的标准ToString()方法

以返回自定义字符串。



背景:



C#中的每个类都是从对象类自动派生的。

ToString()是对象类的方法。

请阅读:

https://msdn.microsoft.com/en-us/library/system.object.tostring%28v=vs.110%29.aspx [ ^ ]



因此一个类自动有一个ToString()方法。



当你重写ToString()方法

你可以返回一个自定义字符串。



You missed the first line.

The method ToString() is used to replace the standard ToString() method
that every object has in order to return a custom string.

Background:

Every class in C# is automatically derived from the object class.
ToString() is a method of the object class.
Please read this:
https://msdn.microsoft.com/en-us/library/system.object.tostring%28v=vs.110%29.aspx[^]

A class therefore automatically has a ToString() method.

When you override the ToString() method
you can return a custom string.


这篇关于使用Arrays实现纸牌游戏。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆