zoukankan      html  css  js  c++  java
  • C# 范型编程

    Using directives

    namespace GenericsSingleton
    {
        
    /// <summary>
        
    /// 单例范型
        
    /// </summary>
        
    /// <typeparam name="T"></typeparam>

        public class Singleton<T>
        
    {
            
    private static T _instance;

            
    public Singleton()
            
    {
            }


            
    public static T Instance
            
    {
                
    get
                
    {
                    
    if (_instance == null)
                    
    {  

                       
    //获得实例,使用这个方法的前提是T要有公有的、无参数的构造函数               

                       _instance 
    = (T)System.Activator.CreateInstance(typeof(T));
                    }

                    
    return _instance;
                }

            }

        }


        
    /// <summary>
        
    /// 要实现单例的类
        
    /// </summary>

        public class Foo
        
    {
            
    private int count = 0;

            
    public int Count
            
    {
                
    get
                
    {
                    count
    ++;
                    
    return count;
                }

            }

        }


        
    /// <summary>
        
    /// 主函数
        
    /// </summary>

        class Program
        
    {
            
    static void Main(string[] args)
            
    {
                
    //连续调用三次如果值是递增的话说明三次调用都是同一个实例
                Console.WriteLine(Singleton<Foo>.Instance.Count);
                Console.WriteLine(Singleton
    <Foo>.Instance.Count);
                Console.WriteLine(Singleton
    <Foo>.Instance.Count);

                Console.ReadLine();
            }

        }

    }

     
  • 相关阅读:
    【幻化万千戏红尘】qianfengDay03-java基础学习:for循环,switch,if
    Hexo | 博客文章链接优化之abbrlink
    Hexo 集成 Gitalk 评论系统
    Linux对一个目录及其子目录所有文件添加权限
    PhpStorm配置使用phpunit单元测试
    php根据当前定位经纬度排序
    mysql根据当前定位经纬度排序
    linux压缩和解压缩命令
    PHP常见数学函数及BC高精度数学函数用法示例
    vagrant package 打包自己的box镜像
  • 原文地址:https://www.cnblogs.com/xiaotuni/p/2365744.html
Copyright © 2011-2022 走看看