DESKTOP-5NB63LS_20200905-潘振林

问题

单例模式里面获得实例的方法是被static修饰的(静态工厂方法)

public final class Moon {
    private static Moon single ;
    private Moon() {
        super();
    }
    public static Moon getInstance() {
        if( single == null ) { 
            single = new Moon();
        }
        return single ;
    }
}//懒汉模式
public final class Earth {
    private static final Earth EARTH = new Earth();
    private Earth() {
        super();
    }
    public static Earth getInstance() {
        return EARTH ;
    }
}//饿汉模式

心得

不到考试永远不会认识到自己学的有多差