Java从入门到精通Chapter7_2单例模式 public class SingleType { // 单例模式 private static SingleType test = null; // 私有的构造方法 private SingleType() {} public static SingleType getInstance() { if(test == null) test = new SingleType(); return test; } // 普通方法输出名字 public void getName() { System.out.println("SingleType"); } public static void main(String[] args) { System.out.println("obj1"); SingleType obj1 = SingleType.getInstance(); obj1.getName(); System.out.println("obj2"); SingleType obj2 = SingleType.getInstance(); obj2.getName(); System.out.println("obj3"); SingleType obj3 = SingleType.getInstance(); obj3.getName(); } }