using System;
using System.Collections.Generic;
using System.Text;namespace apje.frameWork
{
public sealed class singleton<T> where T : new()
{
private static T instance = new T(); private static object lockHelper = new object();
// 构造函数
private singleton()
{ } // 获取实例
public static T getInstance()
{
if (instance == null)
{
lock (lockHelper)
{
if (instance == null)
{
instance = new T();
}
}
} return instance;
} // 设置实例
public void setInstance(T value)
{
instance = value;
}
}
}代码从DZ NT!找回来学习的。为什么红色字部分,要每次初始化呢?我的理解:是不是每次Instance都重新初始化对象呢?那不是浪费吗?
using System.Collections.Generic;
using System.Text;namespace apje.frameWork
{
public sealed class singleton<T> where T : new()
{
private static T instance = new T(); private static object lockHelper = new object();
// 构造函数
private singleton()
{ } // 获取实例
public static T getInstance()
{
if (instance == null)
{
lock (lockHelper)
{
if (instance == null)
{
instance = new T();
}
}
} return instance;
} // 设置实例
public void setInstance(T value)
{
instance = value;
}
}
}代码从DZ NT!找回来学习的。为什么红色字部分,要每次初始化呢?我的理解:是不是每次Instance都重新初始化对象呢?那不是浪费吗?
我知道STATIC啥意思啊