C#设计模式-单例实例
来源:互联网 发布:地大网络教育 编辑:程序博客网 时间:2024/06/03 16:08
一.创建一个自己类型的私有静态变量
二.将构造函数私有化
三.创建一个获取实例的public静态函数
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 单例Tools{ /// <summary> /// 单例 Tools类 /// </summary> public class Tools { /// <summary> ///创建一个自己类型的私有静态变量 /// </summary> private static Tools tools; /// <summary> /// 将构造函数私有化 /// </summary> private Tools() { } /// <summary> /// 创建静态方法 获取实例(单例) /// </summary> /// <returns>Tools</returns> public static Tools GetInstance() { if (tools == null) tools = new Tools(); return tools; } public void showInfo() { Console.WriteLine("这是工具类!"); } }}Program.cs
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 单例Tools{ class Program { static void Main(string[] args) { Tools t = Tools.GetInstance(); t.showInfo(); Console.ReadLine(); } }}
阅读全文
0 0
- C#设计模式-单例实例
- C# 单实例设计模式
- C# 实例 -------单例模式
- 设计模式实例-单例
- C#单例设计模式
- C#单例设计模式
- C#设计模式(单例
- C# 单例设计模式
- C# 单例设计模式
- 设计模式php实例:单例模式
- 设计模式----单例模式 实例
- C#单实例模式
- 设计模式 - 单实例
- 单实例设计模式
- c# 设计模式:单例模式
- c# 设计模式:单例模式
- C#设计模式之单例模式
- C#设计模式-单例模式
- 冒泡排序,选择排序,快速排序
- python之 序列与字典遍历
- Spring教程第三篇:Spring AOP
- 自定义idea File and Code Templates
- Dataframe筛选做法
- C#设计模式-单例实例
- JS和Node.js的部分操作技巧
- Luogu P2910 寻宝之路
- java环境和Tomcat环境
- python变量的拷贝与引用
- 推荐一些不错的公众号【三】
- BZOJ2242 [SDOI2011]计算器 【BSGS】
- 支持向量机SVM(二)
- SaltStack