单例写法两种:
(1)饿汉式 开发用这种方式。
class Student
{
Private Student(){}
private static Student s = new Student();
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;
}
}
|
|