zl程序教程

您现在的位置是:首页 >  其它

当前栏目

结构类型

类型 结构
2023-09-14 09:00:20 时间

实现效果:
  

实现代码:

        static void Main(string[] args)
        {
            Console.WriteLine("***A First Look at Structures***\n");
            //创建初始Point
            Point myPoint = new Point();   //使用默认构造函数将所有字段设置为默认值
            myPoint.x = 199;
            myPoint.y = 99;
            myPoint.Display();
            //调整X和Y
            myPoint.Increment();
            myPoint.Display();
            Console.ReadKey();
        }
        struct Point
        {
            //结构的字段
            public int x;
            public int y;
            //将XY坐标增加1
            public void Increment()
            {
                x++; y++;
            }
            //将XY坐标减去1
            public void Decrement()
            {
                x--; y--;
            }
            //显示当前坐标
            public void Display()
            {
                Console.WriteLine("X={0},Y={1}",x,y);
            }
        }