开发者社区 问答 正文

java单例设计模式中如何实现懒汉式?

已解决

java单例设计模式中如何实现懒汉式?

展开
收起
游客gzyuldo4mrg6i 2022-04-03 18:07:56 983 分享 版权
1 条回答
写回答
取消 提交回答
  • 推荐回答

    class SingletonLazy{

    private static Singleton instance;
    
    private static SingeletonLazy(){}
    
    public static SingletonLazy getInstance() {
    
        if (instance == null) { //此时还没有实例化
    
            instance = new SingeletonLazy();    //实例化对象
    
        }
    
        return instance;
    
    }
    
    public void print(){
    
        System.out.println("Hello world!");
    
    }
    

    }

    2022-04-03 18:11:49
    赞同 展开评论