50,541
社区成员
发帖
与我相关
我的任务
分享
public class testSub {
public static void main(String[] args) {
Key subKey1 = Key.getInstance();
Key subKey2 = Key.getInstance();
Key subKey3 = Key.getInstance();
// 这里创建了3个对象,Key()构造方法只会执行一次
}
}
class Key {
private static Key instance = new Key();
private Key() {
// 类的构造函数定义为private的,保证其他类不能实例化此类,然后提供了一个静态实例并返回给调用者。
System.out.println("!!!!!!!!!");
}
public static Key getInstance() {
return instance;
}
}