class Student
{
//2.步骤2
private static Student stuObj = null;
//1.步骤1
private Student(){
}
//3.步骤3
public static Student getStudentInstance(){
if(stuObj == null){
System.out.println("构造了一个Student对象");
stuObj = new Student();
}
System.out.println("返回一个Student引用");
return stuObj;
}
}
class Demo
{
public static void main(String[] args)
{
Student stu = Student.getStudentInstance();//第一次获取,产生一个对象
Student stu2 = Student.getStudentInstance();//之后再获取,将直接返回引用,不再产生对象
Student stu3 = Student.getStudentInstance();
}
}
|