class Single
{
private Single(){} //私有化构造函数
private static Single s= new Single();//创建一个本类对象
public static Single getInstance()
{
return s;
}
}
class SingleDemo
{
public static void main (String[] args)
{
Single ss = Single.getInstance;
}
}
有两种模式:有必要列举出来
饿汉式
class Single
{
privite static Single x = new Single();
privite static Single(){}
public static Single getInstance()
{
retrun x;
}
}
懒汉式
class Single
{
privit static Single x = null;
privite static Single(){}
public static Single getInstance()
{
if (x ==null)
x = new Single();
retrun x ;
}
}
作者: 王健辉 时间: 2014-8-10 20:01
楼主你看到多线程了吧,好好整理懒汉式,比较常见,要涉及到同步安全问题,双重否定。
送你
class single{