public class Demo {
//单例设计模式,只有且只能有一个对象
public static void main(String[] args) {
System.out.println(Student.getStu());
System.out.println(Student.getStu());
System.out.println(Student.getStu());
}
}
public class Student {
private Student(){
}
//通过Private防止其他类调用
private static Student stu = new Student();
//通过方法调用对象
public static Student getStu(){
return stu;
}
}
运行结果:
com.ithema.test4.Student@3b07d329
com.ithema.test4.Student@3b07d329
com.ithema.test4.Student@3b07d329