让form2获取hxy的唯一实例
class hxy
{
private static hxy hxyInstance;
public static hxy GetInstance()
{
if(hxyInstance == null)
hxyInstance = new hxy();
return hxyInstance;
}
public hxy()
{
...
hxyInstance = this;
}
}
class hxy
{
private static hxy hxyInstance;
public static hxy GetInstance()
{
if(hxyInstance == null)
hxyInstance = new hxy();
return hxyInstance;
}
public hxy()
{
...
hxyInstance = this;
}
}
可能是我说得不清楚吧~~~
////////////////////////////// 独立的hxy类////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
public class hxy
{
public string temp; //为了测试是否可以共享使用
private static hxy hxyInstance;
public static hxy GetInstance()
{
if(hxyInstance == null)
hxyInstance = new hxy();
return hxyInstance;
}
public hxy()
{
hxyInstance = this;
} }
//////////////////////////////////////////////////////////////////////////////
////////////////////////////////// 窗体Form1 /////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
private void Form1_Load(object sender, System.EventArgs e)
{
hxy h=new hxy(); //实例化 hxy class
h.temp="hello world!"; //为了看效果 给temp赋值
Form2 f2=new Form2();
f2.Show();
}
//////////////////////////////////////////////////////////////////////////////
////////////////////////////////// 窗体Form2 /////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
private void Form2_Load(object sender, System.EventArgs e)
{
//这里如何才写 才能调用已经在Form1窗体实例化的hxy类?
}
//////////////////////////////////////////////////////////////////////////////
顺便问一下~~~如何 结此问题~~~嘿嘿~~~新手~~~高手别笑话我 谢谢啦
{
private static hxy hxyInstance = new hxy();
public static hxy HxyInstance
{
return hxyInstance;
}
}
{
public string temp;
private static hxy hxyInstance = new hxy(); public static hxy HxyInstance()
{
return hxyInstance;
}
public static hxy GetInstance()
{
if(hxyInstance == null)
hxyInstance = new hxy();
return hxyInstance;
}
public hxy()
{
hxyInstance = this;
temp="失败~~~";
} }这样写对吗????俺蔡~~~都晕晕了~~~~~~