(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;
}
} |