zoukankan      html  css  js  c++  java
  • 我喜欢的两种单例写法

    1,第一种:

     1 package ToolPackage
     2 {
     3  /**
     4   * 提示
     5   * @author tqr <br />
     6   * 创建时间:2014-11-7 下午6:27:10
     7   */
     8  public class Tip
     9  {
    10   private static  var instanceB:Boolean=true;  
    11   private static var instance:Tip;
    12   
    13   public function Tip()
    14   {
    15    if (instanceB) {  
    16     throw new Error("该类为单例,只能用getInstance()来获取实例");  
    17    }  
    18   }
    19   
    20   public static function getInstance():Tip{
    21    if (!instance) {  
    22     instanceB = false;  
    23     instance = new Tip();  
    24     instanceB = true;  
    25    }  
    26    return instance;  
    27   }
    28   
    29  }
    30 }


    2,第二种:

     1 package ToolPackage
     2 {
     3  /**
     4   * 提示
     5   * @author tqr <br />
     6   * 创建时间:2014-11-7 下午6:27:10
     7   */
     8  public class Tip
     9  {
    10   private static var instance:Tip = new Tip();
    11   
    12   public function Tip()
    13   {
    14    if (instance) {  
    15     throw new Error("该类为单例,只能用getInstance()来获取实例");  
    16    }  
    17   }
    18   
    19   public static function getInstance():Tip{
    20    return instance;  
    21   }
    22   
    23  }
    24 }
  • 相关阅读:
    【CF580D】Kefa and Dishes
    【poj3311】Hie with the Pie
    校外实习-7.7
    校外实习-7.6
    校外实习-7.5
    校外实习-7.4
    作业九-课程总结(补充)
    作业九-课程总结
    作业四——结对编程四则运算
    作业三
  • 原文地址:https://www.cnblogs.com/shuishenwuyu/p/4082018.html
Copyright © 2011-2022 走看看