编程题: 写一个Singleton出来。
方式一:醉汉式
public class Singleton
{
private static Singleton test = new Singleton();//静态的。保留自身的引用。
//必须是私有的构造函数
private Singleton(){}
public static Singleton getInstance()//公共的静态的方法。
{
return test;
}
}
方式二:懒汉式
public class Singleton
{
private static Singleton test = null;//静态的。保留自身的引用。
//必须是私有的构造函数
private Singleton(){}
public static Singleton getInstance()//公共的静态的方法。
{
if(test == null)
{
test = new Singleton();
}
return test;
}
}
面试题
最新推荐文章于 2024-01-20 20:06:43 发布