public static Student getInstance()
{
return s;
}
}
(2)懒汉式 面试写这种方式。
class Teacher
{
private Teacher(){}
private static Teacher t;
public static Teacher getInstance()
{
if(t==null)
{
t = new Teacher();
}
return t;
}
}作者: 张昶 时间: 2013-3-28 21:30
饿汉式:
class Single{
private static final Single s = new Single();
private Single(){}
public static Single getInstance(){
return s;
}
}
懒汉式(延迟加载的单例设计模式):
class Single{
private static Single s = null;
private Single(){}
public static Single getInstance(){
if(s==null){
synchronized(Single.class){
if(s==null){
s = new Single();
}
}
}
return s;
}
}作者: 被遗弃者 时间: 2013-3-28 21:31
都是这么强悍呀作者: 董霁辉 时间: 2013-3-28 21:35
private static final SingleDemo s = new SingleDemo();