自己原创!有错误请指正,转载请注明(首先表示感谢!)
我认为 静态和非静态的区别来源于数据存储,你写的程序是要存储到计算机中的,在运行的时候要调用的,如果有一个参数或者方法只有一个用途,不会再存储和检索,方法不会来回使用,那么这个可以设置成静态的,废话少说上例子
public class CP5200 { private const string m_strPath = "CP5200.dll"; [DllImport(m_strPath, CharSet = CharSet.Auto)] public static extern string CP5200_RS232_GetFileName(); [DllImport(m_strPath, CharSet = CharSet.Auto)] public static extern int CP5200_RS232_SplitScreen(int nCardID, int nScrWidth, int nScrHeight, int nWndCnt, int[] pWndRects); [DllImport(m_strPath, CharSet = CharSet.Auto)] public static extern int CP5200_Net_SplitScreen(int nCardID, int nScrWidth, int nScrHeight, int nWndCnt, int[] pWndRects); [DllImport(m_strPath, CharSet = CharSet.Auto)] public static extern int CP5200_RS232_InitEx(IntPtr fName, int nBaudrate, int dwTimeout); [DllImport(m_strPath, CharSet = CharSet.Auto)] public static extern int CP5200_Net_Init(uint dwIP, int nIPPort, uint dwIDCode, int nTimeOut); [DllImport(m_strPath, CharSet = CharSet.Auto)] public static extern int CP5200_RS232_Open(); [DllImport(m_strPath, CharSet = CharSet.Auto)] public static extern int CP5200_RS232_SendText(int nCardID, int nWndNo, IntPtr pText, int crColor, int nFontSize, int nSpeed, int nEffect, int nStayTime, int nAlignment); [DllImport(m_strPath, CharSet = CharSet.Auto)] public static extern int CP5200_Net_SendText(int nCardID, int nWndNo, IntPtr pText, int crColor, int nFontSize, int nSpeed, int nEffect, int nStayTime, int nAlignment); [DllImport(m_strPath, CharSet = CharSet.Auto)] public static extern int CP5200_RS232_SendTagText(int nCardID, int nWndNo, IntPtr pText, int crColor, int nFontSize, int nSpeed, int nEffect, int nStayTime, int nAlignment); [DllImport(m_strPath, CharSet = CharSet.Auto)] public static extern int CP5200_Net_SendTagText(int nCardID, int nWndNo, IntPtr pText, int crColor, int nFontSize, int nSpeed, int nEffect, int nStayTime, int nAlignment); [DllImport(m_strPath, CharSet = CharSet.Auto)] public static extern int CP5200_RS232_SendPicture(int nCardID, int nWndNo, int nPosX, int nPosY, int nCx, int nCy, IntPtr pPictureFile, int nSpeed, int nEffect, int nStayTime, int nPictRef); [DllImport(m_strPath, CharSet = CharSet.Auto)] public static extern int CP5200_Net_SendPicture(int nCardID, int nWndNo, int nPosX, int nPosY, int nCx, int nCy, IntPtr pPictureFile, int nSpeed, int nEffect, int nStayTime, int nPictRef); [DllImport(m_strPath, CharSet = CharSet.Auto)] public static extern int CP5200_RS232_SendStatic(int nCardID, int nWndNo, IntPtr pText, int crColor, int nFontSize, int nAlignment, int x, int y, int cx, int cy); [DllImport(m_strPath, CharSet = CharSet.Auto)] public static extern int CP5200_Net_SendStatic(int nCardID, int nWndNo, IntPtr pText, int crColor, int nFontSize, int nAlignment, int x, int y, int cx, int cy); [DllImport(m_strPath, CharSet = CharSet.Auto)] public static extern int CP5200_RS232_SendClock(int nCardID, int nWinNo, int nStayTime, int nCalendar, int nFormat, int nContent, int nFont, int nRed, int nGreen, int nBlue, IntPtr pTxt); [DllImport(m_strPath, CharSet = CharSet.Auto)] public static extern int CP5200_Net_SendClock(int nCardID, int nWinNo, int nStayTime, int nCalendar, int nFormat, int nContent, int nFont, int nRed, int nGreen, int nBlue, IntPtr pTxt); [DllImport(m_strPath, CharSet = CharSet.Auto)] public static extern int CP5200_RS232_SetTime(byte nCardID, byte[] pInfo); [DllImport(m_strPath, CharSet = CharSet.Auto)] public static extern int CP5200_Net_SetTime(byte nCardID, byte[] pInfo); [DllImport(m_strPath, CharSet = CharSet.Auto)] public static extern int CP5200_RS232_PlaySelectedPrg(int nCardID, int[] pSelected, int nSelCnt, int nOption); [DllImport(m_strPath, CharSet = CharSet.Auto)] public static extern int CP5200_Net_PlaySelectedPrg(int nCardID, int[] pSelected, int nSelCnt, int nOption); } }
但是如果一个参数或者方法被来回使用,那么这个数据(参数或者方法)存储的时候,机器会根据你的定义(没有STATIC之类的)自动存储为非静态的,带有指针的也就是会把这段程序安排个指针,也就是把这个程序的第一个字节存贮的位置即地址码保存起来,我们使用的时候直接调用这个地址码就可以了,不用再来回调用整个程序了(节省内存啊),废话少说上例子,算了这个就不用上了,到处都是。总结下,静态的直接可以使用如A.add();非静态的必须实例化A a=new A();这也很好理解,静态的就是直接存在的,非静态的是个指针(看我另一篇)一个地址码你能直接引用,你得实例化吧!