• 类和结构的区别


    注意!先看左上角声明!!!谢谢 互相学习!

    类是引用类型,结构是值类型,下面的例子中 大量使用结构类型

    namespace CSharpTest
    {
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        public struct LOGFONTW
        {
            public int lfHeight;
            public int lfWidth;
            public int lfEscapement;
            public int lfOrientation;
            public int lfWeight;
            public byte lfItalic;
            public byte lfUnderline;
            public byte lfStrikeOut;
            public byte lfCharSet;
            public byte lfOutPrecision;
            public byte lfClipPrecision;
            public byte lfQuality;
            public byte lfPitchAndFamily;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
            public string lfFaceName;
        }
        public struct SYSTEMTIME
        {
            public ushort wYear;
            public ushort wMonth;
            public ushort wDayOfWeek;
            public ushort wDay;
            public ushort wHour;
            public ushort wMinute;
            public ushort wSecond;
            public ushort wMilliseconds;
        }
        /// <summary>
        /// Point
        /// </summary>
        public struct NP_POINT
        {
            /// <summary>
            /// //水平起始位置
            /// </summary>
            public int X;
            /// <summary>
            /// 垂直起始位置
            /// </summary>
            public int Y;
        }
        //Size
        public struct NP_SIZE
        {
            //宽度
            public int Width;
            //高度
            public int Height;
        };
        //Rectangle
        public struct NP_RECTANGLE
        {
            //x
            public int X;
            //y
            public int Y;
            //宽度
            public int Width;
            //高度
            public int Height;
        };
    }

    下述代码中两个都使用

     public struct TTimeStamp
        {
            public Int32 time;
            public Int32 date;
        }
        public struct TRoad
        {
            public uint name;
            public uint speed;
        }
    
        class CLEDSender
        {
            //通讯设备类型
            public readonly ushort DEVICE_TYPE_COM = 0;  //串口通讯
            public readonly ushort DEVICE_TYPE_UDP = 1;  //网络通讯
            public readonly ushort DEVICE_TYPE_485 = 2;  //485通讯
    
            //串口或者485通讯使用得通讯速度(波特率)
            public readonly ushort SBR_57600 = 0;
            public readonly ushort SBR_38400 = 1;
            public readonly ushort SBR_19200 = 2;
            public readonly ushort SBR_9600  = 3;
            //道路选择
            public readonly ushort ROAD_1 = 0;
             public readonly ushort ROAD_2 = 1;
            public readonly ushort ROAD_3 = 2;
            //速度选择
            public readonly ushort GREEN = 0;
            public readonly ushort RED = 1;
            public readonly ushort YELLOW = 2;
    
           
    
            //是否等待下位机应答,直接发送所有数据
            public readonly UInt32 NOTIFY_NONE  = 1;
            //是否阻塞方式;是则等到发送完成或者超时,才返回;否则立即返回
            public readonly UInt32 NOTIFY_BLOCK = 2;
            //是否将发送结果以Windows窗体消息方式送到调用得应用
            public readonly UInt32 NOTIFY_EVENT = 4;
    
            public readonly Int32 R_DEVICE_READY    = 0;
            public readonly Int32 R_DEVICE_INVALID  = -1;
            public readonly Int32 R_DEVICE_BUSY     = -2;
            public readonly Int32 R_FONTSET_INVALID = -3;
            public readonly Int32 R_DLL_INIT_IVALID = -4;
            public readonly Int32 R_IGNORE_RESPOND  = -5;
    
            //下位机应答标识
            public readonly ushort LM_RX_COMPLETE = 1;
            public readonly ushort LM_TX_COMPLETE = 2;
            public readonly ushort LM_RESPOND     = 3;
            public readonly ushort LM_TIMEOUT     = 4;
            public readonly ushort LM_NOTIFY      = 5;
            public readonly ushort LM_PARAM       = 6;
            public readonly ushort LM_TX_PROGRESS = 7;
            public readonly ushort LM_RX_PROGRESS = 8;
            public readonly ushort RESULT_FLASH = 0xff;

    下面看下两者的区别

    namespace Ch09Ex03
    {
    class MyClass
    {
    public int val;
    }
    struct myStruct
    {
    public int val;
    }
    class Program
    {
    static void Main(string[] args)
    {
    MyClass objectA = new MyClass();
    MyClass objectB = objectA;
    objectA.val = 10;
    objectB.val = 20;
    myStruct structA = new myStruct();
    myStruct structB = structA;
    structA.val = 30;
    structB.val = 40;
    Console.WriteLine("objectA.val = {0}", objectA.val);
    Console.WriteLine("objectB.val = {0}", objectB.val);
    Console.WriteLine("structA.val = {0}", structA.val);
    Console.WriteLine("structB.val = {0}", structB.val);
    Console.ReadKey();
    }
    }
    }

    运行类是引用类型也就是带有指针的,我们所引用的是指针,也就是地址,MyClass objectB = objectA;也即是两个引用类型共用了一个指针(地址相同),你可以这么试下

      static void Main(string[] args)
            {
                MyClass objectA = new MyClass();
                MyClass objectB = objectA;
                objectA.val = 10;
                //objectB.val = 20;
                myStruct structA = new myStruct();
                myStruct structB = structA;
                structA.val = 30;
                structB.val = 40;
                Console.WriteLine("objectA.val = {0}", objectA.val);
                Console.WriteLine("objectB.val = {0}", objectB.val);
                Console.WriteLine("structA.val = {0}", structA.val);
                Console.WriteLine("structB.val = {0}", structB.val);
                Console.ReadKey();
            }
        }

    给任意一个值赋值,两个就有相同的值,明白否?再补充点,这个类和快捷方式差不多,他是引用类型,桌面上的快捷方式并不是真正的文件,只是指向文件的一个地址(路径),如果你让两个快捷方式相等,那么这个快捷方式就同时指向两个文件了

     

  • 相关阅读:
    sql server中的孤立用户
    BWOA开源项目引子
    海边拾贝
    ABAP syntax_error 错误: form send_cmplx_data_015 does not exist.
    一点总结,手机应用开发前景
    这个周末发生了很多事
    品味单反
    远图(FarMap)使用
    And和手机随想
    远图(FarMap)花絮
  • 原文地址:https://www.cnblogs.com/mamiyiya777/p/5753834.html
Copyright © 2020-2023  润新知