- 枚举
public enum MeEnum {
// 枚举的对象
alipay("st001","alst001"),wechat("st002","wxst002");
String account;
String password;
private MeEnum(String a, String b) {
account = a;
password = b;
}
public static void main(String[] args) {
//调用枚举对象的变量值
// String act = MeEnum.alipay.getAccount();
// User user = new User("hudl");
UserPrincipalNotFoundException u1 = User.getInstance();
System.out.println(u1.getName());
System.out.println(User.getInstance().getName());
System.out.println(User.getInstance().getName());
System.out.println(User.getInstance().getName());
}
- 单例模式
//懒汉式
private static User u;
//饿汉式
private static final User u2 = new User("abc");
//私有化构造方法
private User(String name) {
this.name = name;
}
//提供一个公有的获取单例对象的静态方法
// public static User getInstance() {
// if (u == null) {
// u = new User("abc");
// }
// return u;
// }