zoukankan      html  css  js  c++  java
  • Student : IComparable<Student> 以及逆变和协变

    IComparable<Student>是Student的父类,所以IComparable<Student>可以接收Student。
    但是在使用CompareTo方法的时候,必须传入Student,不允许传入父类IComparable<Student>。
    public interface IComparable<in T>
    public class Student : IComparable<Student>
        {
            public int Id { get; set; }
    
            public int CompareTo(Student other)
            {
                return Id - other.Id;
            }
        }
    
        class Test
        {
            public Test()
            {
                IComparable<Student> student1 = new Student() {Id = 1};
    
                IComparable<Student> student2 = new Student() {Id = 2};
                int result = student1.CompareTo(student2);
            }
        }
  • 相关阅读:
    UVA10891
    UVA10453
    UVA 10201
    UVA10154
    UVA11137
    UVA10617
    UVA10271
    UVA10739
    UVA10306
    节流防抖
  • 原文地址:https://www.cnblogs.com/chucklu/p/10343915.html
Copyright © 2011-2022 走看看