0
点赞
收藏
分享

微信扫一扫

Visual Studio Code搭建VUE开发环境

乐百川 2024-08-11 阅读 29

单例设计模式

懒汉式:
/*
懒汉式
 */
public class Student {
    //创建static修饰的成员变量
    private static Student student;

    //设计私有构造方法
    public Student() {
        super();
    }

    //提供共有的方法
    public static synchronized Student getInstance(){
        if(student==null) {
            student= new Student();
        }
        return student;
    }
}
饿汉式:
/*
饿汉式
 */
public class Student {
    //创建static修饰的成员变量
    private static Student stu=new Student();
    //设计私有构造方法
    public Student() {
        super();
    }
    //提供共有的方法
    public static synchronized Student getInstance(){
        return stu;
    }
}
举报

相关推荐

0 条评论