// 设计模式:单例模式:设计一个类B:要求这个类对外只提供唯一的对象。
public class B {
// (2)
static B b;
// (1)构造方法
private B(){
}
// (3)
public static B getB() {
if (b == null) {
b = new B();
}
return b;
}
public static void main(String[] args) {
B b1 = B.getB();
System.out.println(b1);
B b2 = B.getB();
System.out.println(b2);
}
}