本文介绍了泛型和实现IComparable的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新的仿制药,我尝试写一个简单的类,这将是通用的,但也允许一个字符串成员变量的一些说明排序。

目前,我有一个基本的类,但是当我试图实现接口成员的CompareTo()我得到在上面的错误告诉我,它没有实现。什么是这里的问题?

 使用系统;

命名空间GenericsPracticeConsole.Types
{
    类SortableGenericType< T> :IComparable的
    {
        私人T(T);
        私人字符串字符串名称;

        公共牛逼的名字
        {
            {返回吨; }
            集合{T =价值; }
        }

        公众诠释的CompareTo(SortableGenericType< T> ourObject)
        {
            返回stringName.CompareTo(ourObject.stringName);
        }
    }
}
 

解决方案

有两个接口 IComparable的 IComparable的< U> IComparable的是老一(仿制药之前来),这需要实例与任意的对象进行比较。 IComparable的< U> 需要实例与 U 的实例进行比较。如果你想声明的是,你会比在字符串名称字段SortableGenericType的情况下,这是你应该做的:

 类SortableGenericType< T> :IComparable的< SortableGenericType< T>>
{
   //
}
 

如果您也想实现IComparable:

 类SortableGenericType< T> :IComparable的,IComparable的< SortableGenericType< T>>
   {
      私人字符串字符串名称;
      公共牛逼的名字{获得;组; }

      公众诠释的CompareTo(SortableGenericType< T> ourObject)
      {
         //我忘了补充这一说法:
         如果(ourObject == NULL)
             返回-1;
         返回stringName.CompareTo(ourObject.stringName);
      }

      公众诠释的CompareTo(obj对象)
      {
         如果(obj.GetType()!=的GetType())
            返回-1;
         返回的CompareTo(OBJ为SortableGenericType< T>);
      }
   }
 

如果你的类是将要举办键入 T 的项目的集合,你需要这些物品的订购(这不是你问什么,但它是最常见的情况),比你需要 T IComparable的< T>

 类SomeCollection< T>其中T:IComparable的< T>
   {
      私人列表< T>项目;

      私人无效排序()
      {
         //
         牛逼ITEM1;
         牛逼ITEM2;
         如果(item1.CompareTo(ITEM2)℃,)
         {
            //喇嘛喇嘛
         }
      }
   }
 

I am very new to generics and I am trying to write a simple class which will be generic but also allow sorting of some description on a string member variable.

At the moment I have a basic class but when I try to implement the interface member CompareTo() I get an error at the top telling me it is not implemented. What is the issue here?

using System;

namespace GenericsPracticeConsole.Types
{
    class SortableGenericType<T> : IComparable
    {
        private T t;
        private string stringName;

        public T name
        {
            get { return t; }
            set { t = value; }
        }

        public int CompareTo(SortableGenericType<T> ourObject)
        {
            return stringName.CompareTo(ourObject.stringName);
        }
    }
}
解决方案

There are two interfaces IComparable and IComparable<U>. IComparable is the older one (that came before generics) which requires instances to be compared with arbitrary objects. IComparable<U> requires instances to be compared with instances of U. If you want to declare that you will compare instances of SortableGenericType on stringName fields this is what you should do:

class SortableGenericType<T> : IComparable<SortableGenericType<T>>
{
   //
}

If you also want to implement IComparable:

   class SortableGenericType<T> : IComparable, IComparable<SortableGenericType<T>> 
   {
      private string stringName;
      public T name { get; set; }

      public int CompareTo(SortableGenericType<T> ourObject)
      {
         //I forgot to add this statement:
         if(ourObject == null) 
             return -1; 
         return stringName.CompareTo(ourObject.stringName);
      }

      public int CompareTo(object obj)
      {
         if (obj.GetType() != GetType())
            return -1;
         return CompareTo(obj as SortableGenericType<T>);
      }
   }

If your class was a collection that is going to hold items of type T and you needed those items to be orderable (this is not what you ask but it is the most common scenario) than you would require T to be IComparable<T> :

   class SomeCollection<T> where T : IComparable<T>
   {
      private List<T> items; 

      private void Sort()
      {
         //
         T item1;
         T item2;
         if(item1.CompareTo(item2) < 0)
         {
            //bla bla
         }
      }
   }

这篇关于泛型和实现IComparable的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 22:09