C#编程:创建扑克牌对象实例-用两个for循环
下面说到用循环来创建扑克牌对象并存储于字段.用到了两个for循环.除了大小五共52张牌.
分:红黑花块,A-K
下面看代码示例思想不错.

  1. for (int suitVal = 0; suitVal < 4; suitVal++ )
  2.             {
  3.                 for (int rankVal = 1; rankVal < 14; rankVal++)
  4.                 {
  5.                     cards[suitVal * 13 + rankVal - 1] = new Card((Suit)suitVal, (Rank)rankVal);
  6.                 }
  7.                   
  8.             }

, ,

#编程:异常抛出处理.throw(),try,catch语句
在程序处理时遇到错误.可以采用抛出异常.来让catch语句处理.这三者的关联如何来表达尼.
C#编程:定义类成员-定义属性 这篇文章中说到的;

  1. throw new Exception("err");

抛出一个错误信息err

  1. try
  2.             {
  3.                 //结合 C#编程:定义类成员-定义属性 这篇文章.当给Age属性赋值20时会出错这时抛出异常.后转到catrh.为什么要用try尼.try的用处是:怀疑要执行的代码可能出错用try处理.如果出错就跳转到catch.
  4.                 MyObj.Age = 20;
  5.             }
  6.             catch (Exception e)//Exception e的意思是捕获异常错误代码赋于e这个对象
  7.             {
  8.                 Console.WriteLine(e.Message);
  9.             }

这样整体就很清楚的理解了throw(),try,catch语句

, , , , ,


类属性的定义与定义字段有些相似之处.但结构代码要多出不少.属性一般与一个私有字段相关联,以控制对这个字段的访问,此时get块可以直接返回该字段的值.
看代码示例:

  1. class MyBase
  2.     {
  3.         private int age;
  4.         public int Age
  5.         {
  6.             get
  7.             {
  8.                 return age;
  9.             }
  10.             set
  11.             {
  12.                 age = value;
  13.             }
  14.         }
  15.     }
  16.     class Program
  17.     {
  18.         static void Main(string[] args)
  19.         {
  20.             MyBase MyObj = new MyBase();
  21.             MyObj.Age = 20;
  22.             Console.WriteLine(MyObj.Age);
  23.             Console.ReadKey();
  24.         }
  25.     }

Read the rest of this entry »

, , , , ,

C#编程:定义类成员-定义方法
接着总结定义类成员-定义方法.定义方法通常有四个关键字.来决定方法的使用区域.
关键字:virtual, abstract:, override, extern, static
1.virtual:方法可以重写
2.abstract:只用于抽象类中重写
3.override:方法重写了一个基类的方法
4.extern:方法定义放在其它地方

定义方法实例:

  1. class MyClass
  2.     {
  3.         public string GetString()
  4.         {
  5.             return "here is a string.";
  6.         }
  7.     }

使用此方法:先实例化对象

  1. static void Main(string[] args)
  2.         {
  3.             MyClass MyObj = new MyClass();
  4.             Console.WriteLine(MyObj.GetString());
  5.             Console.ReadKey();
  6.         }

Read the rest of this entry »

, , , , , , ,