zoukankan      html  css  js  c++  java
  • 黑马程序员C#泛型简介

    ---------------------- Windows Phone 7手机开发.Net培训、期待与您交流! ----------------------

    泛型:通过参数化类型来实现在同一份代码上操作多种数据类型。利用“参数化类型”将类型抽象化,从而实现灵活的复用。

    例子代码:

    class Program

        {

            static void Main(string[] args)

            {

                int obj = 2;

                Test<int> test = new Test<int>(obj);

                Console.WriteLine("int:" + test.obj);

                string obj2 = "hello world";

                Test<string> test1 = new Test<string>(obj2);

                Console.WriteLine("String:" + test1.obj);

                Console.Read();

            }

        }

     

        class Test<T>

        {

            public T obj;

            public Test(T obj)

            {

                this.obj = obj;

            }

    }

        输出结果是:

        int:2

    String:hello world

     

    程序分析:

      Test是一个泛型类。T是要实例化的范型类型。如果T被实例化为int型,那么成员变量obj就是int型的,如果T被实例化为string型,那么obj就是string类型的。

      根据不同的类型,上面的程序显示出不同的值。

    ---------------------- Windows Phone 7手机开发.Net培训、期待与您交流! ----------------------

  • 相关阅读:
    Length of Last Word
    Remove Duplicates from Sorted Array II
    Sum Root to Leaf Numbers
    Valid Parentheses
    Set Matrix Zeroes
    Symmetric Tree
    Unique Binary Search Trees
    110Balanced Binary Tree
    Match:Blue Jeans(POJ 3080)
    Match:Seek the Name, Seek the Fame(POJ 2752)
  • 原文地址:https://www.cnblogs.com/victorruan/p/2829395.html
Copyright © 2011-2022 走看看