为什么在一个类A中调用另一个类B的函数,A类中不要定义B类的实例,即可B类中的函数
为什么在一个类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?
------解决思路----------------------
static 关键字
static的意思是成员是class级别的,而不是object级别的。
隐含的意思就是不论 new了多少个objects, static成员只有一份,所有objects共享。
------解决思路----------------------
静态成员,是不需要实例的
------解决思路----------------------
因为是 public static,静态函数,静态函数调用不需要实例化,直接 类名.xxxx
------解决思路----------------------
静态类和静态函数的调用不需要实例化
------解决思路----------------------
把static去掉就要实例化了
------解决思路----------------------
B类定义静态方法
------------------------------------------------------------------------------------------------
请为参加博客之星的我投上一票感激之至
------解决思路----------------------
楼上说的没有错,static这个关键字起作用了
------解决思路----------------------
public static
------解决思路----------------------
只有用静态了(public static)
代码如下
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?
------解决思路----------------------
static 关键字
static的意思是成员是class级别的,而不是object级别的。
隐含的意思就是不论 new了多少个objects, static成员只有一份,所有objects共享。
------解决思路----------------------
静态成员,是不需要实例的
------解决思路----------------------
因为是 public static,静态函数,静态函数调用不需要实例化,直接 类名.xxxx
------解决思路----------------------
静态类和静态函数的调用不需要实例化
------解决思路----------------------
把static去掉就要实例化了
------解决思路----------------------
B类定义静态方法
------------------------------------------------------------------------------------------------
请为参加博客之星的我投上一票感激之至
------解决思路----------------------
楼上说的没有错,static这个关键字起作用了
------解决思路----------------------
public static
------解决思路----------------------
只有用静态了(public static)