package com.dodoke.demo0723;
public class Earth {
//定义私有构造方法
private Earth(){
System.out.println("地球诞生");
}
//定义私有静态类对象并完成实例化
private static Earth ground=new Earth();
//定义公有静态方法返回类内的私有静态对象
public static Earth getGround(){
return ground;
}
}
package com.dodoke.demo0722;
import com.dodoke.demo0723.Earth;
public class Etest {
public static void main(String[] args) {
System.out.println("第一个地球创建中。。。");
Earth one=Earth.getGround();
System.out.println("第二个地球创建中。。。");
System.out.println("第三个地球创建中。。。");
System.out.println("问:三个地球是同一个么?");
System.out.println();
}
}
单例模式
最新推荐文章于 2023-07-26 15:46:48 发布