public class Sun {
// 在 Sun 类内部创建该类的实例 ( 自己内部早出自己的实例 )
private final static Sun SUN=new Sun();
// 将构造方法私有化,避免在 Sun 类外部创建 Sun 实例
private Sun(){
super();
}
// 提供一个 类方法 用来返回 Sun 类的惟一实例
public static Sun getInstance(){
return SUN;
}
}
2、 懒汉式 实现 单例模式 ( 不考虑 线程安全问题 和 线程并发问题 )
public class Earth {
//声明一个私有的唯一变量用于存储Earth实例
private static Earth earth;
//为了不让外部类随意创建Earth实例,将构造方法私有化
private Earth(){
super();
}
//声明一个公开的静态方法,用于创建并返回唯一实例
public static Earth getInstance(){
if(earth==null){
earth=new Earth();
}
return earth;
}
}
1、/** 单例模式 ( 饿汉式 ) */public class Sun { // 在 Sun 类内部创建该类的实例 ( 自己内部早出自己的实例 ) private final static Sun SUN=new Sun(); // 将构造方法私有化,避免在 Sun 类外部创建 Sun 实例 private Sun(){ super();...