使用任意编程语言,写一个单例模式的类
//懒汉模式
public class Singleton{
//持有唯一实例的私有静态变量
private static Singleton instance;
//私有构造函数,防止外部实例化
private Singleton(){};
//提供公共静态方法可以获得单例
public static Singleton getInstance(){
if(instance == null){
instance = new Singleton();
}
return instance;
}
} import threading class Single(object): _instance_lock = threading.Lock() def __init__(self): pass def __new__(cls, *args, **kwargs): if not hasattr(Single, "_instance"): with Single._instance_lock: if not hasattr(Single, "_instance"): Single._instance = Single(*args, *kwargs) return Single._instance