public class User{ private User(){};//构造方法要私有化! private static User user;//类对象要私有化! public static User getOnly(){ if(user == null){ user = new User(); } return user; } }
单例模式,”构造方法“和”类对象“要私有化!!