zoukankan      html  css  js  c++  java
  • 泛型那点儿事儿 泛型概述 简单样例代码

    
    /*
    什么是泛型? 如何使用和定义泛型?
        泛型是具有占位符(类型参数)的类、结构、接口和方法,
        这些占位符是类、结构、接口和方法所存储或使用的一个或多个类型的占位符。
        泛型集合类可以将类型参数用作它所存储的对象的类型的占位符;
        类型参数作为其字段的类型和其方法的参数类型出现。
        泛型方法可以将其类型参数用作其返回值的类型或者其形参的类型之一。
        摘自《Microsoft .NET Framework SDK v2.0 文档》
        泛型概述
        ms-help://MS.NETFramework.v20.chs/dv_fxfund/html/c92e7fb8-bc97-4a06-88f9-2066c58431fa.htm
        http://msdn.microsoft.com/zh-cn/library/ms172193(VS.95).aspx
    */
    namespace ConsoleApplication
    {
        using System;
        using Microshaoft;
        public class Class1
        {
            static void Main(string[] args)
            {
                //GenericClass<int, string> x = new GenericClass<int, string>();
                GenericClass<int, string, Test> x = new GenericClass<int, string, Test>();
                x.Add<string>(1, "a");
                x.Add<int>(11, "b");
                x.Print<int>();
                x.Print(new Test("aaaaaaaaaaaaaaaaaaa"));
                Console.WriteLine("Hello World");
                Console.WriteLine(Environment.Version.ToString());
            }
            
        }
        class Test : ITest
        {
            private string _m = "";
            public Test (string x)
            {
                _m = x;
            }
            public void Aa()
            {
                Console.WriteLine(_m);
            }
        }
    }
    namespace Microshaoft
    {
        using System;
        using System.Collections.Generic;
        public class GenericClass<T1, T2, T5>
                                    where T5 : ITest
                                    where T2 : class
        {
            private Dictionary<T1, T2> _dictionary = new Dictionary<T1, T2>();
            
            public void Add<T3>(T1 x,T2 y)
            {
                T2 z = null;
                z = default(T2);
                this._dictionary.Add(x, y);
            }
            public void Print<T4>()
            {
                foreach(T1 var in this._dictionary.Keys)
                {
                    Console.WriteLine("{0},{1}", var, _dictionary[var]);
                }
            }
            public void Print(T5 x)
            {
                x.Aa();
            }
        }
        public interface ITest
        {
            void Aa();
        }
    }
    // CS0403.cs
    // CS0304.cs
    // compile with: /target:library
    class C<T>
            where T : new ()
    {
        public void f()
        {
            //T t = null;  // CS0403
            T t2 = default(T);   // OK
            t2 = new T();
        }
    }
    class D<T> where T
                        : class
                            , new ()
    {
        public void f()
        {
            T t = null;  // OK
            t = new T();
        }
    }
    
    
  • 相关阅读:
    jquery源码 DOM加载
    用 Vue 全家桶二次开发 V2EX 社区
    java中初始化对象变量的方法
    跟我一起学extjs5(08--自己定义菜单1)
    NYOJ 57 6174问题
    Android 手动按power键上锁,没有锁屏提示音,无法恢复【单机必现】
    说好的加班呢
    排序总结之高速排序
    【c语言】模拟实现库函数的atof函数
    Oracle存储过程update受外键约束的主键值时完整性冲突解决方式
  • 原文地址:https://www.cnblogs.com/Microshaoft/p/1528210.html
Copyright © 2011-2022 走看看