我有一个A
实现单例模式的类并包含object obj
:
public sealed class A
{
static A instance=null;
static readonly object padlock = new object();
public object obj;
A()
{
AcquireObj();
}
public static A Instance
{
get
{
if (instance==null)
{
lock (padlock)
{
if (instance==null)
{
instance = new A();
}
}
}
return instance;
}
}
private void AcquireObj()
{
obj = new object();
}
}
现在我有另一个 B 类,我需要在其中保留 A.obj 对象的实例,直到它还活着。
public class B
{
// once class A was instantiated, class B should have public A.obj
// field available to share.
// what is the best way/practice of putting obj here?
}
谢谢你。