饿汉单例模式:
class Student{
private static Student s = new Student();
private Student(){
}
public static Student getInstance(){
return s;
}
}
//懒汉单例模式
class Person{
private static Person p;
private person(){
}
public static Person getinstance(){
if(p==null){
p=new Person();
}
return p;
}
}
本文介绍了两种常见的单例模式实现方式:饿汉式和懒汉式。饿汉式单例模式在类加载时就创建实例,而懒汉式单例模式则在第一次调用时创建实例。这两种方式各有优缺点,在不同的应用场景下选择合适的单例模式实现。
1519

被折叠的 条评论
为什么被折叠?



