unity游戏开发-C#语言基础篇(结构函数)

 class Program
    {
        static void Main(string[] args)
        {
            int[] arr = new int[] { 2, 4, 56, 7 };

            // Console.WriteLine(arr[1]);//index 索引器

            HeroClass hero = new HeroClass();

            hero[0] = "黑暗战神";


            //Console.WriteLine(hero[0]);

            string[] str = new string[] { "光明小怪", "非人类", "白混蓝" };
            for (int i = 0; i < str.Length; i++)
            {
                hero[i] = str[i];
                //Console.Write(hero[i] + " ");
            }

            Console.WriteLine("怪物名字:{0},性别:{1},肤色:{2}", hero[0], hero[1], hero[2]);


            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    if(j==1){
                        Console.WriteLine("aaa");
                        break;

                    }

                    if (j == 1)
                    {

                        continue;
                        Console.WriteLine("bbb");
                    }


                    if (j == 1)
                    {


                        Console.WriteLine("bbb");
                        return;
                    }
                }
            }



            //-----------
            //GC.Collect();//非特殊情况不可调用




            // Rectangle rec = new Rectangle(2,5);
            //rec.GetMianJi(); 


            //结构实例化 值引用

            A m = new A(3);
            A n = m;
            m.x = 10;
            Console.WriteLine(n.x);
            Console.WriteLine(m.x);



            //类实例化//引用类以最后一次值为准 引用类会改变它的初始值

            B m1 = new B(30);
            B n1 = m1;

            m1.y = 100;//m1把有改变了,n1也跟着改变


            Console.WriteLine(n1.y);
            Console.WriteLine(m1.y);

            Console.ReadKey();

        }
    }
    struct A
    {
        public int x;

        public A(int _x)
        {
            this.x = _x;
            Console.WriteLine("x={0}", x);

        }
    }
  class B
    {
        public int y;
        public B(int _y)
        {

            this.y = _y;
            Console.WriteLine("y={0}", this.y);
        }

    }
 class HeroClass
    {
        private string name;//0
        private string sex;//1
        private string fuse;//2

        private int age;

        public string this[int index]
        {
            set
            {
                switch (index)
                {
                    case 0:
                        this.name = value;
                        break;

                    case 1:
                        this.sex = value;
                        break;
                    case 2:
                        this.fuse = value;
                        break;
                    default:
                        Console.WriteLine("超出范围");
                        break;

                }

            }
            get
            {
                string a = "";
                //switch (index)
                //{
                //    case 0:
                //        a= this.name;
                //        break;

                //    case 1:
                //        a = this.sex;
                //        break;
                //    case 2:
                //        a = this.fuse;
                //        break;
                //    default:
                //       a= "超出范围";
                //        break;
                //}
                //return a;


                switch (index)
                {
                    case 0:
                        a = this.name;
                        break;

                    case 1:
                        a = this.sex;
                        break;
                    case 2:
                        a = this.fuse;
                        break;
                    default:
                        a = "超出范围";
                        break;
                }
                return a;


            }
        }
    }
 struct Rectangle  //结构函数
    {
        private int chang;//结构函数不能设置值;普通类的可以;
        private int kuan;

        public int Kuan
        {
            get { return kuan; }
            set { kuan = value; }
        }

        public int Chang
        { //属性

            set { this.chang = value; }
            get { return this.chang; }

        }

        public Rectangle(int _chang, int _kuan)
        {//必须带参的构造函数。实例化时可不带参

            this.chang = _chang;
            this.kuan = _kuan;

        }


        public void GetMianJi()
        {

            Console.WriteLine("面积:" + this.chang * this.kuan);
        }


    }

你可能感兴趣的:(unity游戏开发-C#语言基础篇(结构函数))