//单例模式
public class Indicator {
//构造方法私有化
private Indicator(){
}
/*
如果不是静态变量,则外部类无法拿到此变量
Indicator indicator = new Indicator();
本类内部创建的对象没有任何意义,因为无法赋值给外部类
所以必须声明为静态才可以有机会被外部类拿到
当然为了程序安全就可以将indicator进行封装,通过get方法获得
*/
private static Indicator indicator = new Indicator();
public static Indicator getIndicator() {
return indicator;
}
public void test(){
System.out.println("hello world");
}
}
单例模式
最新推荐文章于 2025-04-29 15:30:59 发布