楼主: Lisrelchen
1737 18

C# Design Patterns: A Tutorial [推广有奖]

  • 0关注
  • 62粉丝

VIP

已卖:4194份资源

院士

67%

还不是VIP/贵宾

-

TA的文库  其他...

Bayesian NewOccidental

Spatial Data Analysis

东西方数据挖掘

威望
0
论坛币
50288 个
通用积分
83.6306
学术水平
253 点
热心指数
300 点
信用等级
208 点
经验
41518 点
帖子
3256
精华
14
在线时间
766 小时
注册时间
2006-5-4
最后登录
2022-11-6

楼主
Lisrelchen 发表于 2016-5-22 02:24:12 |AI写论文
1论坛币
  • C# Design Patterns: A Tutorial
  • By: James W. Cooper

  • Publisher: Addison-Wesley Professional

  • Pub. Date: September 17, 2002

  • Print ISBN-10: 0-201-84453-2

  • Print ISBN-13: 978-0-201-84453-5

  • Pages in Print Edition: 416

  • Subscriber Rating: [1 Rating]



关键词:Tutorial Patterns Pattern Design tutor Design

沙发
Lisrelchen 发表于 2016-5-22 02:24:42
  1. Converting between Numbers and Strings

  2. To make a string from a number or a number from a string, you can use the Convert methods. You can usually find the right one by simply typing “Convert” and a dot in the development environment, and the system will provide you with a list of likely methods.

  3. string s = Convert.ToString (x);
  4. float y = Convert.ToSingle (s);
复制代码

藤椅
Lisrelchen 发表于 2016-5-22 02:25:29
  1. Declaring Multiple Variables

  2. You should note that in C# you can declare a number of variables of the same type in a single statement.

  3. int i, j;
  4. float x, y, z;
复制代码

板凳
Lisrelchen 发表于 2016-5-22 02:26:02
  1. Numeric Constants

  2. Any number you type into your program is automatically of type int if it has no fractional part or type double if it does. If you want to indicate that it is a different type, you can use various suffix and prefix characters.

  3. float loan = 1.23f;       //float
  4. long pig   = 45L;         //long
  5. int color = 0x12345;      //hexadecimal
复制代码

报纸
Lisrelchen 发表于 2016-5-22 02:28:33
  1. A Simple Temperature Conversion Program
  2. private void> btCompute_Click(object sender,
  3.               System.EventArgs e) {
  4.        float temp, newTemp;
  5.        //convert string to input value
  6.        temp = Convert.ToSingle (txEntry.Text );
  7.        //see which scale to convert to
  8.        if(opFahr.Checked)
  9.               newTemp = 9*temp/5 + 32;
  10.        else
  11.               newTemp = 5*(temp-32)/9;
  12.        //put result in label text
  13.        lbResult.Text =newTemp.ToString ();
  14.        txEntry.Text ="";   //clear entry field
  15. }
复制代码

地板
Lisrelchen 发表于 2016-5-22 02:30:12

Building a Temperature Class

  1. Building a Temperature Class
  2. public class Temperature   {
  3.        private float temp, newTemp;
  4.        //-------------
  5.        //constructor for class
  6.        public Temperature(string thisTemp)            {
  7.              temp = Convert.ToSingle(thisTemp);
  8.        }
  9.        //-------------
  10.        public string getConvTemp(bool celsius){
  11.        if (celsius)
  12.               return getCels();
  13.        else
  14.               return getFahr();
  15.        }
  16.        //-------------
  17.        private string getCels() {
  18.               newTemp= 5*(temp-32)/9;
  19.               return newTemp.ToString() ;
  20.        }
  21.        //-------------
  22.        private string getFahr() {
  23.               newTemp = 9*temp/5 + 32;
  24.               return Convert.ToString(newTemp) ;
  25.        }
  26. }
复制代码

7
Lisrelchen 发表于 2016-5-22 02:32:03

Putting the Decisions into the Temperature Class

  1. public string getConvTemp(){
  2.        if (celsius)
  3.               return getCels();
  4.        else
  5.               return getFahr();
  6. }
  7. //-------------
  8. private string getCels() {
  9.        newTemp= 5*(temp-32)/9;
  10.        return newTemp.ToString() ;
  11. }
  12. //-------------
  13. private string getFahr() {
  14.        newTemp = 9*temp/5 + 32;
  15.        return Convert.ToString(newTemp) ;
  16. }
复制代码

8
Lisrelchen 发表于 2016-5-22 02:33:15

Using Classes for Format and Value Conversion

  1. public FormatTime(string entry)          {
  2. errflag = false;
  3. if (! testCharVals(entry)) {
  4.        int i = entry.IndexOf (":");
  5.        if (i >= 0 ) {
  6.               mins = Convert.ToInt32 (entry.Substring (0, i));
  7.               secs = Convert.ToSingle (entry.Substring (i+1));
  8.               if(secs >= 60.0F ) {
  9.                      errflag = true;
  10.                      t = NT;
  11.               }
  12.               t = mins *100 + secs;
  13.        }
  14.        else {
  15.               float fmins = Convert.ToSingle (entry) / 100;
  16.               mins = (int)fmins;
  17.               secs = Convert.ToSingle (entry) - 100 * mins;
  18.               if (secs >= 60) {
  19.                      errflag = true;
  20.                      t = NT;
  21.               }
  22.               else
  23.                      t = Convert.ToSingle(entry);
  24.                 }
  25.               }
  26. }
复制代码


9
Lisrelchen 发表于 2016-5-22 02:34:17

A String Tokenizer Class

  1. //String Tokenizer class
  2. public class StringTokenizer      {
  3.   private string data, delimiter;
  4.   private string[] tokens; //token array
  5.   private int index;              //index to next token
  6. //----------
  7. public StringTokenizer(string dataLine)                {
  8.        init(dataLine, " ");
  9. }
  10. //----------
  11. //sets up initial values and splits string
  12. private void init(String dataLine, string delim) {
  13.        delimiter = delim;
  14.        data = dataLine;
  15.        tokens = data.Split (delimiter.ToCharArray() );
  16.        index = 0;
  17. }
  18. //----------
  19. public StringTokenizer(string dataLine, string delim) {
  20.        init(dataLine, delim);
  21. }
  22. //----------
  23. public bool hasMoreElements() {
  24.        return (index < (tokens.Length));
  25. }
  26. //-----------
  27. public string nextElement() {
  28.        //get the next token
  29.        if( index > tokens.Length )
  30.               return tokens[index++];
  31.        else
  32.               return "";    //or none
  33.        }
  34. }
复制代码


10
Lisrelchen 发表于 2016-5-22 02:35:49

Classes as Objects

  1. public class Swimmer {
  2.        private string frName, lName;
  3.        private string club;
  4.        private int age;
  5.        private int place;
  6.        private FormatTime tms;
  7. //-----------
  8. public Swimmer(String dataLine) {
  9.        StringTokenizer tok = new StringTokenizer (dataLine);
  10.        place =  Convert.ToInt32 (tok.nextElement());
  11.        frName = tok.nextElement ();
  12.        lName = tok.nextElement ();
  13.        string s = tok.nextElement ();
  14.        age = Convert.ToInt32 (s);
  15.        club = tok.nextElement ();
  16.        tms = new FormatTime (tok.nextElement ());

  17. }
  18. //-----------
  19. public string getName() {
  20.        return frName+" "+lName;
  21. }
  22. //-----------
  23. public string getTime() {
  24.        return tms.getTime();
  25. }
  26. }
复制代码

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2025-12-22 03:09