zoukankan      html  css  js  c++  java
  • 泛型委托示例

    delegate T Factory<out R, in S , T>() 

    //   out R  协变        in  S   逆变     T   不变

    ----------------------------------------------------------------------------------------------------

    public delegate TR Func<T1, T2, TR>(T1 p1, T2 p2);   //泛型委托  TR委托返回类型   T1,T2 委托参数类型

    class Simple

    {

      static public string PrintString(int p1, int p2) //方法匹配委托

      {

        int total = p1 + p2;

        return total.ToString();

      }

    }

    class Program

    {

      static void Main()

        {

          var myDel = new Func <int, int, string>(Simple.PrintString);   //创建委托实例

          Console.WriteLine("ToTal: {0}", myDel(15, 13));//  调用委托

        }

    }

    另一种out协变

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApplication33
    {
        class Animal                                            //基类
        {
            public int Legs = 4;
        }

        class Dog:Animal                                        //派生类
        {

        }

        class Program
        {
            delegate T Factory<out T>();

            static Dog MackDog() { return new Dog(); }
            static void Main(string[] args)
            {
                Factory<Animal> animalMacker = MackDog;             //隐式强制转换

                Factory<Dog> dogMacker = MackDog;

                Factory<Animal> animalMacker2 = dogMacker;          //需要out标识符

                Factory<Animal> animalMacker3 = new Factory<Dog>(MackDog);  //需要out标识符
            }
        }
    }

  • 相关阅读:
    大数据学习之大数据简介03
    大数据学习之Linux进阶02
    大数据学习之Linux基础01
    连接数据库出现java.sql.SQLException: Unknown system variable 'tx_isolation'
    Linux中伪分布的搭建
    【TCP/IP】入门学习笔记 三
    【TCP/IP】入门学习笔记 二
    【TCP/IP】入门学习笔记 一
    【CentOS】CentOS7 自动同步时间:服务ntp,命令ntpdate
    【Mysql】- pt-online-schema-change在线更新大表字段、加索引
  • 原文地址:https://www.cnblogs.com/bedfly/p/11921520.html
Copyright © 2011-2022 走看看