1. C#語言基礎-lesson#5 枚舉+結構體(枚舉,結構體,訪問修飾符)

    2021-03-10 20:11發布

    C#語言基礎

    lesson#5 結構體+結構體

    內容:

    1. 枚舉,結構體,訪問修飾符前言;

    2. 枚舉;

    3. 結構體;

    1,前言: 枚舉,結構體,訪問修飾符都要寫在class之前
    //枚舉:又稱為一一列舉,把要取的值列舉出來,然后在列舉的值里面選擇要賦的值

    //枚舉一般定義在類的外面,語法格式:

    //enum枚舉類型名{值1,值2...}

    //enum是關鍵字,用來定義一個枚舉類型的,自定義的類型名字首字母均要大寫

    //系統會給枚舉值賦一個默認的int類型的值,第一個枚舉值為0,第二個為1,以此類推,因此枚舉值和int類型的值之間可以進行轉換

        enum Sex

        {

            ,

            

        }

    //練習:定義一年四季的枚舉

    //定義人的動作的枚舉

     

    //可以根據解決問題的實際需求,人為的更改枚舉值

        enum Season

        {

            Spring=1,

            Summer,

            Autumn,

            Winter

        }

        enum PlayerStatus

        {

            Run,

            Shoot,

            Jump,

            Throw,

            Eat,

            Dance

        }

        enum LP

        {

            Normal=2,

            Magic=10

        }

        enum Department

        {

            市場部=800,

            財務部=900

        }

        enum GameStatus

        {

            開始游戲,

            暫停游戲,

            結束游戲

        }

        enum Week

        {

            Monday=1,

            Tuesday,

            Wednesday,

            Thursday,

            Friday,

            Saturday,

            Sunday

        }

        enum Things

        {

            上課,

            吃飯,

            約會,

            運動,

            打手衛生,

            看電影,

            看書    

        }

    //結構體語法格式:

    /*

    struct 類型名

     {

    成員

     }

     */

    //public是訪問修飾符,它規定了結構體或者類里的訪問權限,結構體里面的變量和方法統一稱為結構體的成員,變量稱為字段,public所修飾的成員,訪問不受限制

        struct PersonInfo

        {

            public string name;

            public Sex sex;

            public int age;

            public string address;

        }

        struct StudentsInfo

        {

            public string name;

            public int age;

            public string StudentID;

            public double score;

        }

        struct Tool

        {

    //public 表示該成員是公有的,訪問不受限制,即在結構體或者類的內部和外部均可被訪問

            public double PI;

    //private表示該成員是私有的,只能在結構體或者類的內部訪問,外部不可被訪問

    //在結構體或類的里面,成員前面不加任何訪問修飾符,該成員默認是私有的。

            private double radius;

        }

    2,枚舉

    //int a = 10;

                Sex sex = Sex.男;

    //枚舉有自注釋的作用

                Season sea = Season.Spring;

                PlayerStatus playstatus = PlayerStatus.Dance;

    //要求:從鍵盤輸入1,打印春天,輸入2打印夏天,輸入3打印秋天,輸入4打印冬天;

                Console.WriteLine((int)sex);

    //把一個int類型的值,轉成枚舉值

                Console.WriteLine((Season)3);

     

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

                Season season = (Season)count;

     

                //if (season==1)

                //{

                //    Console.WriteLine("春天");

                //}

                //if (season == 2)

                //{

                //    Console.WriteLine("夏天");

                //}

                //if (season == 3)

                //{

                //    Console.WriteLine("秋天");

                //}

                //if (season == 4)

                //{

                //    Console.WriteLine("冬天");

     

                if (season == Season.Spring)

                {

                    Console.WriteLine("春天");

                }

                if (season == Season.Summer)

                {

                    Console.WriteLine("夏天");

                }

                if (season == Season.Autumn)

                {

                    Console.WriteLine("秋天");

                }

                if (season == Season.Winter)

                {

                    Console.WriteLine("冬天");

                }

     

                switch (season)

                {

                    case Season.Spring:

                        Console.WriteLine("春天");

                        break;

                    case Season.Summer:

                        Console.WriteLine("夏天");

                        break;

                    case Season.Autumn:

                        Console.WriteLine("秋天");

                        break;

                    case Season.Winter:

                        Console.WriteLine("冬天");

     

                        break


    日日摸夜夜添夜夜添无码,free性欧美极度另类,超清中文乱码字幕在线观看,真人美女两腿张开露PP图片