从每个表单上的公共类获取实例 [英] get instance from public class on each form

查看:65
本文介绍了从每个表单上的公共类获取实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好



我有这个班级



命名空间用户

{

公共类ApplicationData

{

私有静态ApplicationData appData = null;

private string nesto;

public static ApplicationData getInstance()

{

if(appData == null)

{

appData = new ApplicationData();

}

返回appData;

}



公共字符串Nesto

{

get

{



返回nesto;

}

set {nesto = value; }

}



}

}


$ b我所有表格上的$ b我必须使用Nesto,但我不知道如何将这个类调用到我的表单

Hello

I have this class

namespace User
{
public class ApplicationData
{
private static ApplicationData appData = null;
private string nesto;
public static ApplicationData getInstance()
{
if (appData == null)
{
appData = new ApplicationData();
}
return appData;
}

public string Nesto
{
get
{

return nesto;
}
set { nesto = value; }
}

}
}

on all my forms I have to use Nesto but I don't know how to call this class to my forms

推荐答案

单例是一个类它只允许创建自己的单个实例,并且通常可以简单地访问该实例

对于Singleton,您还应该将构造函数设置为private。您可以在不创建对象的情况下调用getInstance。因为该方法是静态的。

例如

A singleton is a class which only allows a single instance of itself to be created, and usually gives simple access to that instance
For Singleton you should also set constructor as private. You can call the getInstance without creating an object.Because the method is static.
For example
ApplicationData obj= ApplicationData.getInstance();





第一次创建 ApplicationData 类的实例。然后再次尝试调用它将限制的这个过程。

希望这有帮助



It will create an instance of ApplicationData class on the first time. Then again you try to call this process it will restrict .
Hope this helps


这篇关于从每个表单上的公共类获取实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆