[Quote=引用 3 楼 lwb314 的回复:]
举个例子,你是个程序员,你希望任何时候有一个class A的实例就可以了,所以你在你的class B里封装了一个A的对象
class B
{
static A a = new A();
}
这样以后所有代码大家都可以使用B.a来调用A里的方法。很方便,可是没几个月你走了,换我接手
我想使用A里的方法,可是也许我根本就没有你的API,我不知道B里已经有了一个static的A,因此我在我的……
[/Quote]
你的理解是错误的,看一面代码:
public class D {
private D() {
new A();
}
private static D inst;
public static D getInst() {
if (inst == null) {
return new D();
}
return inst;
}
}
public class E {
private E() {
new A();
}
private static E inst;
public static E getInst() {
if (inst == null) {
return new E();
}
return inst;
}
}