zoukankan      html  css  js  c++  java
  • 单例类

    某个类,仅当系统不存在自身类的实体才创建实体。  即,系统中要么无实体,要么仅有一个实体。

     1 class Singleton
     2 {
     3     //使用一个变量来缓存曾经创建的实例
     4     private static Singleton instance;
     5     //将构造器使用private修饰,隐藏该构造器
     6     private Singleton(){}
     7     //提供一个静态方法,用于返回Singleton实例
     8     //该方法可以加入自定义的控制,保证只产生一个Singleton对象
     9     public static Singleton getInstance()
    10     {
    11         //如果instance为null,表明还不曾创建Singleton对象
    12         //如果instance不为null,则表明已经创建了Singleton对象,将不会执行该方法
    13         if (instance == null)
    14         {
    15             //创建一个Singleton对象,并将其缓存起来
    16             instance = new Singleton();
    17         }
    18         return instance;
    19     }
    20 }
    21 public class TestSingleton
    22 {
    23     public static void main(String[] args)
    24     {
    25         //创建Singleton对象不能通过构造器,只能通过getInstance方法
    26         Singleton s1 = Singleton.getInstance();
    27         Singleton s2 = Singleton.getInstance();
    28         //将输出true
    29         System.out.println(s1 == s2);
    30     }
    31 }
  • 相关阅读:
    L1范数和L2范数
    Python---scikit-learn(sklearn)模块
    Python------SciPy模块
    Python---Pandas模块
    Python---NumPy模块---矩阵操作
    Python---NumPy模块
    sift代码实现详解
    opencv 图像
    OpenCV-Mat结构详解
    OpenCV3+VS2015 经常出现debug error abort()has been called问题
  • 原文地址:https://www.cnblogs.com/joyeehe/p/7886966.html
Copyright © 2011-2022 走看看