class Student : IComparable<Student>
    {
        private string stuName;         //学生姓名
        private int mathScore;          //高数成绩
        private int englishScore;       //英语成绩
        private int specScore;          //专业课成绩        public Student(string name, int math, int english, int spec)
        {
            this.stuName = name;
            this.mathScore = math;
            this.englishScore = english;
            this.specScore = spec;
        }        public int TotalScore()
        { 
        //计算总成绩的方法
        }        //实现泛型接口中的方法
        public int CompareTo(Student other)
        {
            return this.TotalScore().CompareTo(other.TotalScore());
        }
    }
这样写的泛型类 具体有什么用?class Student : IComparable<Student>  继承了IComparable接口  public int CompareTo(Student other)
        {
            return this.TotalScore().CompareTo(other.TotalScore());
        }
这句话又怎么理解啊?
传进去一个student对象other? 用这个类的初始化总分 和 other对象的总分进行比较?  
那为什么 这里要用 CompareTo比较啊? 不是用==么?  查了下泛型的资料 不是太明白.
高手指点下....... 可能太笨了我!!!

解决方案 »

  1.   

    返回值:
    小于零--此实例小于 value。 
    零------此实例等于 value。 
    大于零--此实例大于 value- 或 -value 为空引用(Visual Basic 中为 Nothing)。
      

  2.   

    这样写的泛型类 具体有什么用?class Student : IComparable <Student>  继承了IComparable接口 
    继承了一个泛型接口。
      public int CompareTo(Student other) 
            { 
                return this.TotalScore().CompareTo(other.TotalScore()); 
            } 
    这句话又怎么理解啊? 
    传进去一个student对象other? 用这个类的初始化总分 和 other对象的总分进行比较?  
    那为什么 这里要用 CompareTo比较啊? 不是用==么?  查了下泛型的资料 不是太明白. 
    高手指点下....... 可能太笨了我!!!这是实例方法,需要通过实例来调用的,这里的this指代发出调用的对象。
    ==对于引用类型来说,不重载的话,就只是等价于ReferenceEquals方法。