日期:2014-05-20 浏览次数:21017 次
public class SingLeton{
private static SingLeton instance = new SingLeton();
public static SingLeton getInstance(){
return instance;
}
}
public class Singleton {
private static Singleton instace = null; // private 修饰,避免外部更改
private Singleton(){} // 隐藏构造函数
public static synchronized Singleton getInstance() { // 同步,避免同时创建多个实例
if (instance == null) {
instance = new ...
}
return instance;
}
}
------解决方案--------------------
public class Singleton{
private static Singleton instance = new Singleton();
private SingLeton(){
}
public static Singleton getInstance(){
return instance;
}
}