zoukankan      html  css  js  c++  java
  • 泛型接口

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace 泛型
    {
        class Program
        {
            static void Main(string[] args)
            {
                //实例化接口
                IGenericl<System.ComponentModel.IListSource> factory =
                    new Factory<System.Data.DataTable, System.ComponentModel.IListSource>();
                //输出指定泛型的类型
                Console.WriteLine(factory.CreateInstance().GetType().ToString());
                Console.ReadLine();
            }
        }
    
        public interface IGenericl<T>
        {
            T CreateInstance();//接口中调用CteateInstance方法
        }
    
        //实现上面泛型接口的泛型类
        //派生约束where T : TI(T要继承自TI)
        //构造函数约束where T : new() (T 可以实例化)
        public class Factory<T, TI> : IGenericl<TI> where T : TI,new()
        {
            public TI CreateInstance()//创建一个公共方法CreateInstance
            {
                return new T();
            }
        }
    
    }
  • 相关阅读:
    codevs1288 埃及分数
    codevs1792 分解质因数
    dp
    JAVA大数贪心
    求最长不重叠子串
    初识后缀数组
    dp
    两数相除,判断小数位是否有限位
    构造二分图匹配
    建立多个树状数组
  • 原文地址:https://www.cnblogs.com/cppwen/p/3327583.html
Copyright © 2011-2022 走看看