package com.leo;
/**
* 饿汉式单例模式 好处:是线程安全的 坏处:加载时间过长
*/
public class SingleTonTest1 {
public static void main(String[] args) {
Bank bank1 = Bank.getInstance();
Bank bank2 = Bank.getInstance();
System.out.println(bank1 == bank2);
}
}
class Bank {
// 1.私有化构造器
private Bank() {
}
// 2.内部创建类的静态对象
private static Bank instance = new Bank();
// 3.提供公共的静态方法, 返回类的对象
public static Bank getInstance() {
return instance;
}
}