泛型接口的定义与使用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace InterImp
{
    class Program
    {
        interface Inter  //定义泛型接口Inter
        {
            void show(T t); //约定功能show
        }
        class InterImpA : Inter  //定义接口Inter的子类InterImpA,明确泛型类型为String
        {
            public void show(String t)  //子类InterImpA重写方法show,指明参数类型为String
            {
                Console.WriteLine(t);
            }
        }
        class InterImpB : Inter //定义接口Inter的子类InterImpB,直接声明泛型
        {
            public void show(T t) //子类InterImpB重写方法show,参数类型为泛型
            {
                Console.WriteLine(t);
            }
        }
        static void Main(string[] args)
        {
            InterImpA i=new InterImpA(); //实例化InterImpA
            i.show("fffff");
            InterImpB j=new InterImpB(); //实例化InterImpB
            j.show(666);
            Console.Read();
        }
    }
}

你可能感兴趣的:(泛型接口的定义与使用)