c#学习

                                                                                 c#小随笔

              1  不加static的方法必须将类进行实例化再可以进行调用,而加static的方法可以直接使用

              2 产生对象必须通过new关键字产生对象

Eg: person   minjie=new person();

3 c#中的访问修饰符 有public  在任何地方被访问

 Private 只能在本类中被访问

Protected  只能在本类中和子类中被访问

Intemal  只能在本项目中被访问

4 使用构造方法的好处

对多个属性进行赋值时,不需要重复写实例名

可以保证用户在new一个对象的时候必须对某一个属性进行赋值

5 多态

如果子类和父类的方法名重名了,会报绿线,如何把绿线干掉?

第一:我们可以在子类的方法的访问修饰符后面加new;

第二:我们可以在父类的方法上加一个virtual  然后 子类在继承父类的时候,可以用override重写父l类的方法

 

 

6 接口(interface)

注:1 接口中可以有属性,方法

2 接口的名称通常以“I”开头,如IList

3 如果一个类即继承了类又实现了接口,那么类必须写在前面。一个类只能继承一个父类,可以实现多个接口

Eg:class Student:Person,Ifly,Iswim

和第2类似,在创建对象时,对只读属性进行初始化

7 容易忽略的地方

1 注释可以出现在代码的任意位置,但不能分分隔关键字和标识符

2 c#中所有Main方法都必须是静态的原因:

C#是一种面向对象的编程语言,即使是程序的启动入口点,她也是一个类的成员,由于程序启动时还没有创建类的对象,因此,必须将入口点Main方法定义为静态方法,使她可以不依赖类的创建对象而执行(静态方法只要定义了类,不必建立类的实例就可使用。静态方法只能用类的静态成员。)

3 format 参数由多个文本序列与零或多个索引占位符混合组成,其中索引占位符成为格式项,他们与此方法的参数列表中的对象相对应。格式设置过程将每个格式项替换成相应对象值的文本表现形式。格式项的语法是{索引[对齐方式][:格式字符串]},他指定一个强制索引,格式化文本的可选长度和对齐方式,以及格式说明符可选字符串,其中格式说明符用于控制如何设置相对应像的值的格式。

 

    DateTime dt = DateTime.Now;

            string str = String.Format("{0:d}",dt);

            Console.WriteLine(str);

            Console.ReadKey();

 

 

8 骑士飞行棋的代码

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace 骑士飞行棋

{

    class Program

    {

        static int[] Map = new int[100];

        static int[] playerPos = {0,0};//playerpos{0}存玩家A是的坐标,playerPos{1}存玩家B的坐标

        static string[] name = new string[2];

        static bool[] isStop = {false,false};//isStop[0]表示A是否走到了暂停,isStop[1]表示B走到了暂停        

        static void Main(string[] args)

        {                  

            showUI();

            Console.WriteLine("请输入玩家A的姓名:");

            name[0] = Console.ReadLine();

            while (name[0] == " ")

            {

                Console.WriteLine("玩家A的姓名不能为空,请重新输入!");

                name[0] = Console.ReadLine();

            }

            Console.WriteLine("请输入玩家B的姓名:");

            name[1] = Console.ReadLine();

            while (name[1] == "" || name[1] == name[0])

            {

                if (name[1] == "")

                {

                    Console.WriteLine("玩家B的姓名不能为空,请重新输入!");

 

                }

                if (name[1] == name[0])

                {

                    Console.WriteLine("该用户已经存在,请重新输入!");

                }

                name[1] = Console.ReadLine();

            }

            Console.Clear();

            showUI();

            Console.WriteLine("对战开始。。。");

            Console.WriteLine("{0}用A来表示", name[0]);

            Console.WriteLine("{0}用B来表示", name[1]);

            Console.WriteLine("如果AB在同一位置,用<>来表示", name[1]);

            initialMap();//初始化地图

            DrawMap();//绘制地图

            Console.WriteLine("开始游戏...");

 

            //这个循环让玩家A和玩家B轮流扔色子,当玩家A或玩家B的坐标》=99时,循环结束

            //循环条件就是

            while(playerPos[0]<99&&playerPos[1]<99)

            {

                if(isStop[0]==false)

                {

                    Action(0);

                }

                else

                {

                    //说明isStop==true

                    isStop[0]=false;

                }                 

                if (playerPos[0] >= 99)

                  {

                      break;

                  }

 

                if(isStop[1]==false)

                {

                   Action(1);

                }

                else

                {

                    //说明isStop==true

                    isStop[1]=false;

                }

            }

            //判断谁胜利谁失败

            Console.Clear();

            showUI();

            if (playerPos[0] >= 99)

            {

                Console.WriteLine("************玩家{0}胜利了**************",name[0]);

            }

            if (playerPos[1] >= 99)

            {

                Console.WriteLine("************玩家{0}胜利了**************", name[1]);

            }

 

            Console.ReadKey(true);

        }

 

        ///<summary>

        /// A或B扔色子的方法 A传0过来 B传0过来

        /// </summary>

      static void Action(int playerNumber)

      {

               //playnumber中存的就是当前玩家 姓名 坐标 是否暂停 这三个数组的下标

               // 当前0,对方1

               //当前1,对方0

                int step = 0;  //用于存放产生的随机数

                string meg = "";//用于存储用户踩到某关卡输出的话

                Random r = new Random();//r是产生随机数用的 

                Console.WriteLine("{0}按任意键开始键开始扔色子",name[playerNumber]);

                Console.ReadKey(true);

                ConsoleKeyInfo rec = Console.ReadKey(true);//获取用户按下的下一个字符或功能键,按下的键显示在窗口

                step = r.Next(1, 7);//产生一个1-6之间的随机数

                if (rec.Key == ConsoleKey.Tab)

                {

                    ConsoleKeyInfo cc = Console.ReadKey(true);

                    if (cc.Key == ConsoleKey.F1)

                    {

                        step = ReadInt(1, 100);

                    }

                }

                  Console.WriteLine("{0}扔出了{1}",name[playerNumber],step);

                  Console.WriteLine("按任意键开始行动");

                  Console.ReadKey(true);

                  playerPos[playerNumber] += step;//注意一旦坐标发生改变,就要判断 坐标值是否>99或者<0

                  CheckPos();//判断坐标是否越界

                  Console.Clear();

                  if (playerPos[0] == playerPos[1])//玩家A踩到玩家B

                  {

                      playerPos[1-playerNumber] = 0;

                      meg = string.Format("{0}踩到了{1},{1}退回原点",name[playerNumber],name[1-playerNumber]);

                  }

                  else

                  {//没踩到,要判断玩家A现在所在的位置是否还有其他关卡

                      switch (Map[playerPos[playerNumber]])

                      {

                          case 0:

                              //普通,没有效果

                              meg = "";

                              break;

                          case 1:

                              //走到了 幸运轮盘关卡

                              Console.Clear();

                              DrawMap();

                              Console.WriteLine("{0}走到了幸运轮盘,请选择运气?",playerPos[playerNumber]);

                              Console.WriteLine("1--交换位子 2--轰炸对方");

                              int userSelect = ReadInt(1,2);

                              if (userSelect == 1)

                              {//要与对方交换位子

                                  int temp = playerPos[0];

                                  playerPos[0] = playerPos[1];

                                  playerPos[1] = temp;

                                  meg = string.Format("{0}选择了与对方交换位置!",name[playerNumber]);

                              }

                              else

                              {

                                  //轰炸对方

                                  playerPos[1-playerNumber] = playerPos[1-playerNumber] - 6;

                                  CheckPos();

                                  meg = string.Format("{0}选择轰炸了{1},{1}退6格",name[playerNumber],name[1-playerNumber]);

                              }

                              break;

                          case 2:

                              //踩到了地雷

                              playerPos[playerNumber] = playerPos[playerNumber] - 6;

                              CheckPos();

                              meg = string.Format("{0}猜到了地雷,退6格!",name[playerNumber]);

                              break;

                          case 3:

                              //暂停一次

                              isStop[0]=true;

                               meg = string.Format("{0}走到了红灯,下次暂停一次",name[playerNumber]);

                              break;

                          case 4:

                              //时空隧道

                              playerPos[playerNumber] = playerPos[playerNumber] + 10;

                             // CheckPos();

                              meg = string.Format("{0}进入时空隧道,爽死了,进10步!",name[playerNumber]);

                              break;

                      }

                  }

                  //Console.WriteLine("按任意键开始行动。。。。。。");

                  Console.ReadKey(true);

                  Console.Clear();

                  DrawMap();

                  if (meg != "")

                  {

                      Console.WriteLine(meg);

                  }

                  Console.WriteLine("{0}扔出了{1},行动完成:",name[playerNumber],step);

                  Console.WriteLine("*************************玩家A和玩家B的位子如下***********************");

                  Console.WriteLine("{0}的位置为:{1}",name[0],playerPos[0]+1);

                  Console.WriteLine("{0}的位置为:{1}",name[1],playerPos[1]+1);

                  Console.ReadKey();

      }

 

        /// <summary>

        /// 用于绘制飞行棋的名称

        /// </summary>

        static void showUI()

        {

            Console.WriteLine("##################################");

            Console.WriteLine("#                                #");

            Console.WriteLine("#      骑 士 飞 行 棋            #");

            Console.WriteLine("#                                #");

            Console.WriteLine("##################################");

        }

 

        ///<summary>

        ///进行玩家A和玩家B坐标越界的判断

        ///</summary>

        static void CheckPos()

        {

            for (int i = 0; i < 2; i++)

            {

                if (playerPos[i] > 99)

                {

                    playerPos[i] = 99;

                }

                if (playerPos[i] < 0)

                {

                    playerPos[i] = 0;

                }

            }

        }

 

        /// <summary>

        /// 对地图中的关卡进行初始化

        /// </summary>

        static void initialMap()

        {

            int[] luckyTurn = { 6, 23, 40, 55, 69, 83 };//幸运轮盘 1

            int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };//地雷 2

            int[] pause = { 9, 27, 60, 93 };//暂停的坐标 3

            int[] TimeTunnel = { 20, 25, 45, 63, 72, 88, 90 };//时空隧道 4

            for (int i = 0; i < luckyTurn.Length; i++)

            {

                int pos = luckyTurn[i];

                Map[pos] = 1;

            }

            for (int i = 0; i < landMine.Length; i++)

            {

                Map[landMine[i]] = 2;

            }

            for (int i = 0; i < pause.Length; i++)

            {

                int pos = luckyTurn[i];

                Map[pos] = 3;

            }

            for (int i = 0; i < TimeTunnel.Length; i++)

            {

                Map[TimeTunnel[i]] = 4;

            }

        }

 

        /////<summary>

        /////获得第pos坐标上应该绘制的元素

        /////以<pos>为坐标显示</pos>

        ///// </summary>

        static string getMapString(int pos)

        {

            string result = "";

            if (playerPos[0] == pos && playerPos[1] == pos)

                {

                    Console.ForegroundColor = ConsoleColor.Yellow;

                    result = ("<>");

                }

            else if (playerPos[0] == pos)

                {

                    Console.ForegroundColor = ConsoleColor.Yellow;

                    result = ("A");//A在当前画格上

                }

            else if (playerPos[1] == pos)

                {

                    Console.ForegroundColor = ConsoleColor.Yellow;

                    result = ("B");//B在当前画格上

                }

                else

                {

                    switch (Map[pos])

                    {

                          

                        case 0:

                            Console.ForegroundColor = ConsoleColor.White;

                            result = "□";//普通

                            break;

                        case 1:

                            Console.ForegroundColor = ConsoleColor.Red;

                            result = "¤";  //幸运轮盘

                            break;

                        case 2:

                            Console.ForegroundColor = ConsoleColor.Green;

                            result = "★";//地雷

                            break;

                        case 3:

                            result = "▲";//暂停

                            break;

                        case 4:

                            Console.ForegroundColor = ConsoleColor.DarkBlue;

                            result = "卍";//时空隧道

                            break;

                    }

                }

            return result;

        }

 

        /// <summary>

        /// 画地图

        /// </summary>

        static void DrawMap()

        {

            Console.WriteLine("图例:幸运轮盘 ¤ 地雷 ★ 暂停▲ 时空隧道 卍 ");          

                //画第一行

                for (int i = 0; i <= 29; i++)

                {

                 Console .Write(getMapString(i));

                }

                Console.WriteLine();

                //画第一列

                for (int i =30; i <= 34; i++)

                {

                    for (int j = 0; j <29; j++)

                    {

                        Console.Write("  ");                   

                    }

                    Console.WriteLine(getMapString(i));

                }

                for (int i=64; i>=35; i--)

                {

                    Console.Write(getMapString(i));

                }

                Console.WriteLine();

                for (int i = 65; i <= 69; i++)

                {

                    Console.WriteLine(getMapString(i));

                }

                for (int i = 70; i <100; i++)

                {

                    Console.Write(getMapString(i));

                }

                Console.WriteLine();

                Console.ResetColor();

            }

 

        /// <summary>

        /// 用户输入只能是1-2之间的方法

        /// </summary>

        static int ReadInt()

        {

            int i = ReadInt(int.MinValue,int.MaxValue);

            return i;

        }

        static int ReadInt(int min, int max)

        {

            while (true)

            {

                try

                {

                    int number = Convert.ToInt32(Console.ReadLine());

                    if (number < min || number > max)

                    {

                        Console.WriteLine("只能输入{0}~{1}之间的数", min, max);

                        continue;

                    }

                    return number;

                }

                catch

                {

                    Console.WriteLine("只能输入数字,请重新输入!");

                }

            }

        }

        }

    }

 

 

 

 

原文地址:https://www.cnblogs.com/GoodPMj/p/4851457.html