为什么在一个类A中调用另一个类B的函数,A类中不用定义B类的实例,即可B类中的函数

代码如下

namespace mw_rdp

{

public class IC

{

[DllImport("Mwic_32.dll", EntryPoint="ic_init",  SetLastError=true,

CharSet=CharSet.Auto , ExactSpelling=false,

CallingConvention=CallingConvention.StdCall)]

public static extern int ic_init(Int16 port, int baud);

}

}

namespace mw_rdp

{

public class Form1 : System.Windows.Forms.Form

{

private void btn_Click(object sender, System.EventArgs e)

{

icdev = IC.ic_init(port, baud);

}

}

}

为什么class Form1类中没有定义class IC的实例,即可以调用class IC中的函数ic_init?











因为是 public static,静态函数,静态函数调用不需要实例化,直接 类名.xxxx

static 关键字

static的意思是成员是class级别的,而不是object级别的。

隐含的意思就是不论 new了多少个objects, static成员只有一份,所有objects共享。

你可能感兴趣的:(为什么在一个类A中调用另一个类B的函数,A类中不用定义B类的实例,即可B类中的函数)