簡體   English   中英

類比較器<T> :存在的目的 IComparer.Compare 的顯式實現

[英]Class Comparer<T>: Aim of existence the Explicit Implementations of IComparer.Compare

存在的目的是在Comparer<T>類的實現中 IComparer.Compare 的顯式實現。 如果我實現了public abstract int Compare (T x, T y)什么原因和在哪里它可以使用int IComparer.Compare (object ObjX, object ObjY)的調用,考慮到 ObjX & ObjY 必須可以被轉換鍵入 T(在其他情況下將是 ArgumentException)。

第 12 行和第 13 行將產生相同的動作;

class Program
{
    static void Main(string[] args)
    {
      IComparer iRefCommon = (IComparer)new BoxLengthFirst();
      object obj1 = new Box(2, 6, 8);
      object obj2 = new Box(10, 12, 14);
      int resulCompare = iRefCommon.Compare((Box)obj1, (Box)obj2); //line12
      resulCompare = (new BoxLengthFirst()).Compare(new Box(2, 6, 8),
             new Box(10, 12, 14)); //line13
    }
}

public class BoxLengthFirst : Comparer<Box> 
{
    public override int Compare(Box x, Box y)
    {
        if (x.Length.CompareTo(y.Length) != 0)
        {
            return x.Length.CompareTo(y.Length);
        }
        .....
        else
        {
            return 0;
        }
    }
}

public class Box : IComparable, IComparable<Box>
{
    public Box(int h, int l, int w)
    {
        this.Height = h;
        this.Length = l;
        this.Width = w;
    }
    public int Height { get; private set; }
    public int Length { get; private set; }
    public int Width { get; private set; }

    public int CompareTo(object obj)
    {
     ....
    }
    public int CompareTo(Box other)
    {
       ....
    }
}

嘗試以下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;


namespace ConsoleApplication108
{
    class Program
    {
        static void Main(string[] args)
        {
            Box a = new Box(1, 1, 1);
            Box b = new Box(2, 2, 2);

            int c = a.CompareTo(b);

        }

    }
    public class Box : IComparable
    {
        public Box() { }
        public Box(int h, int l, int w)
        {
            this.Height = h;
            this.Length = l;
            this.Width = w;
        }
        public int Height { get; private set; }
        public int Length { get; private set; }
        public int Width { get; private set; }

        public int CompareTo(object obj)
        {
             return CompareTo((Box)obj);
        }
        public int CompareTo(object obj1, object obj2)
        {
             return ((Box)obj1).CompareTo((Box)obj2);
        }
        public int CompareTo(Box other)
        {
            int results = this.Height.CompareTo(other.Height);
            if (results != 0)
            {
                results = this.Length.CompareTo(other.Length);
                if (results != 0)
                {
                    results = this.Width.CompareTo(other.Width);
                }
            }
            return results;
        }
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM