zoukankan      html  css  js  c++  java
  • 泛型方法的定义

     泛型方法定义如下。

        public class Printer
        {
            
    /*
             * 本例为泛型方法
             * T是泛型类实例所存储类型的占位符。在泛型类型的实例定义中,必需指定这个实例存储的实际类型。         * 
             
    */
            
    public void Print<T>(T argument)
            {
                
    if (typeof(T)==typeof(string))
                {
                    Console.WriteLine(argument);
                }
                
    else
                {
                    Console.WriteLine(argument.ToString());
                }
            }
        }


     调用方法为:

        class Program
        {
            
    static void Main(string[] args)
            {
                Printer print 
    = new Printer();
                Console.WriteLine(
    "String Type:");
                print.Print
    <string>("Hello");
                Console.WriteLine(
    "Int Type:");
                print.Print
    <int>(100);

                Console.ReadKey();
            }
        }


  • 相关阅读:
    分享
    慕课网-软件测试基础-学习笔记
    向量内积(点乘)和外积(叉乘)概念及几何意义
    使用opencv3+python实现视频运动目标检测
    解决opencv3运行opencv2代码时报错的修改备忘录
    分享
    OpenCV学习笔记
    LeetCode
    LeetCode
    npm安装包很慢
  • 原文地址:https://www.cnblogs.com/scottckt/p/1821817.html
Copyright © 2011-2022 走看看