枚举方式创建单例对象

public class TestSingleton {

    private TestSingleton(){}

    public static TestSingleton getInstance(){
        return SingletonEnum.INSTANCE.getTest();
    }

    private enum SingletonEnum{
        INSTANCE;

        private TestSingleton singleton = null;
        SingletonEnum(){
            singleton = new TestSingleton();
        }

        private TestSingleton getTest(){
            return singleton;
        }

    }
}

 

posted @ 2018-09-08 11:18  qqq齐  阅读(420)  评论(0编辑  收藏  举报