zoukankan      html  css  js  c++  java
  • C# 泛型接口

    使用泛型可以定义接口,在接口中定义的方法可以带泛型参数。

    比如,我们要实现一个IComparable接口来对两个类的对象的属性进行比较。传统的我们会这么做:

    public class Person : IComparable  
    {  
        public string LastName { get; set; }  
      
        public Person(string lastName) { this.LastName = lastName; }  
      
        public int CompareTo(object obj)  
        {  
            Person other = obj as Person; //这里需要做强制转换,然后才能对属性进行比较  
            return this.LastName.CompareTo(other.LastName);  
        }  
    }  

    我们看一下引入泛型接口之后,会发生什么变化:

    同样的我们还是要实现IComparable接口,不同的是这是一个泛型接口

    public class Person : IComparable<Person>  
    {  
        public string LastName { get; set; }  
      
        public Person(string lastName) { this.LastName = lastName; }  
      
        public int CompareTo(Person other)  
        {  
            return this.LastName.CompareTo(other.LastName);//这里可以直接对属性进行操作,不需要之前的转换过程了  
        }  
    }  

    然后就可以在main函数中测试一下上面的代码:

    static void Main(string[] args)  
    {  
        Person[] person = new Person[] { new Person("Microsoft"), new Person("google") };  
        int result = person[0].LastName.CompareTo(person[1].LastName);  
        Console.WriteLine(result);//输出1  
    }  

    上述的例子说明了一点,泛型接口就是带泛型类型的接口,与普通的接口相比,多了类型的约束。.NET1.0就有了基于对象的IComparable接口,IComparable<in T>基于一个泛型类型:

    public interface IComparable<in T>  
    {  
        int CompareTo(T other);  
    }  

    接下来,讲两个与泛型接口有关的两个概念:协变和抗变。

    先定义一个基类Shape:

    public class Shape  
    {  
        public double Width { get; set; }  
        public double Height { get; set; }  
      
        public override string ToString()  
        {  
            return string.Format("{0},height:{1}", Width, Height);  
        }  
    }  

    再定义一个泛型接口:泛型类型用out关键字标注,表明泛型接口是协变的(也就是说返回类型只能是T),并从一个只读索引器中返回这个类型。

    public interface IDisPlay<out T>  
    {  
        T this[int index] { get; }  
    }  

    接下来定义一个Rectangle子类继承Shape类,并实现泛型接口IDisPlay(out T)。

    public class Rectangle : Shape, IDisPlay<Shape>  
    {  
        Shape IDisPlay<Shape>.this[int index]  
        {  
            get  
            {  
                if (index != 0)  
                {  
                    throw new ArgumentOutOfRangeException("index");  
                }  
                else  
                {  
                    this.Width = 400;  
                    this.Height = 500;  
                    return this;  
                }  
            }  
        }  
    }  

    最后我们测试一下结果:

    static void Main(string[] args)  
    {  
        IDisPlay<Shape> shapeDisplay = new Rectangle();//把实现了泛型接口的类对象赋值给泛型接口  
        Console.WriteLine(shapeDisplay[0].ToString());//通过泛型接口的索引器访问,返回这个对象  
    }  

    确实输出了我们想要的结果:400,height:500.

    如果泛型类型用in关键字标注,泛型接口就是抗变的。

  • 相关阅读:
    Git忽略规则.gitignore梳理
    基于Spring + Spring MVC + Mybatis 高性能web构建
    Java语言主要特点有哪些?
    PHP,JAVA,NET 开发比较
    Linux系统SVN安装指导配置说明
    SSM三大框架整合详细教程(Spring+SpringMVC+MyBatis)
    Java Service Wrapper简介与使用
    js 使用tsx文件 引入node_modules安装的包文件提示 未找到模块(默认已经安装此包文件)
    js 压缩图片
    配置 ts-loader 后 执行编译时报错 TS6142: Module './router' was resolved to 'C:GitHubWorkspacewebpackPluginDemosrc outer.tsx', but '--jsx' is not set.
  • 原文地址:https://www.cnblogs.com/guwei4037/p/5639503.html
Copyright © 2011-2022 走看看