以下代码是as3实现的单态类,当然还有其他的实现方式。

 

代码
package{
    
/**
        单态类示例
    
*/
    
public class Singleton{
        
//记录已经存在的实例
        private static var _instance:Singleton = null;
        
        
public function Singleton(){
            
if(_instance == null){
                _instance 
= this;
            }
else{
                
throw Error("Error: Singleton类是单态类,只能创建一个实例");
            }
        }
        
//获取单态类的实例
        public function getInstance():Singleton{
            
if(_instance != null){
                
return _instance;
            }
            
return new Singleton();
        }
    }
}

 

 

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-12-11
  • 2021-08-21
  • 2022-01-15
  • 2022-12-23
猜你喜欢
  • 2021-07-27
  • 2022-12-23
  • 2021-09-27
  • 2021-07-28
  • 2022-12-23
  • 2021-05-20
  • 2021-12-17
相关资源
相似解决方案