包含类型没有实现接口和System.IComparable'错误

时间:2015-01-18 18:05:04

标签: c# sorting inheritance compareto icomparable

namespace Theprogram.cs
{
    class Program
    {
        static void Main(string[] args)
        {
            CreditCustomer[] creditCustomer = new CreditCustomer[5];
            int x, y;
            bool goodNum;
            for (x = 0; x < creditCustomer.Length; ++x)
            {
                creditCustomer[x] = new CreditCustomer();
                Console.Write("Enter customer number ");
                creditCustomer[x].CustomerNumber = Convert.ToInt32(Console.ReadLine());
                goodNum = true;
                for (y = 0; y < x; ++y)
                {
                    if (creditCustomer[x].Equals(creditCustomer[y]))
                        goodNum = false;
                }
                while (!goodNum)
                {
                    Console.Write("Sorry, the customer number " +
                       creditCustomer[x].CustomerNumber + " is a duplicate. " +
                       "\nPlease reenter ");
                    creditCustomer[x].CustomerNumber = Convert.ToInt32(Console.ReadLine());
                    goodNum = true;
                    for (y = 0; y < x; ++y)
                    {
                        if (creditCustomer[x].Equals(creditCustomer[y]))
                            goodNum = false;
                    }
                }
                Console.Write("Enter name ");
                creditCustomer[x].CustomerName = Console.ReadLine();
                Console.Write("Enter age ");
                creditCustomer[x].Rate = Convert.ToInt32(Console.ReadLine());
                Console.Write("Enter amount due ");
                creditCustomer[x].AmountDue = Convert.ToDouble(Console.ReadLine());
            }
            Array.Sort(creditCustomer);
            Array.Sort(customer);
        }
    }
    class Customer : IComparable<Customer>
    {
        private int customerNumber;
        public int CustomerNumber
        {
            get
            {
                return customerNumber;
            }
            set
            {
                customerNumber = value;
            }
        }
        private string customerName;
        public string CustomerName
        {
            get
            {
                return customerName;
            }
            set
            {
                customerName = value;
            }
        }
        private double amountDue;
        public double AmountDue
        {
            get
            {
                return amountDue;
            }
            set
            {
                amountDue = value;
            }
        }
        public Customer(int num, string name, int amt)
        {
        CustomerNumber = num;
        CustomerName = name;
        AmountDue = amt;
        }
        public Customer(): this(9, "ZZZ", 0)
        {
        }
        public override bool Equals(Object e)
        {
            bool equal;
            Customer temp = (Customer)e;
            if (this.CustomerNumber == temp.CustomerNumber)
                equal = true;
            else
                equal = false;
            return equal;
        }
        public override int GetHashCode()
        {
            return CustomerNumber;
        }
        public override string ToString()
        {
            return (GetType() + " Credit Customer " + CustomerNumber + " " + CustomerName +
              " Amount Due is " + AmountDue.ToString("C") + " Interest rate is ");
        }
        protected virtual int IComparable.CompareTo(Object o)
        {
            int returnVal;
            Customer temp = (Customer)o;
            if (this.CustomerNumber >
                temp.CustomerNumber)
                returnVal = 1;
            else
                if (this.CustomerNumber < temp.CustomerNumber)
                    returnVal = -1;
                else
                    returnVal = 0;
            return returnVal;
        }
    }
    class CreditCustomer : Customer, IComparable<CreditCustomer>
    {
        public int Rate {get; set;}
        int MonthlyPayment { get; set; }
        public CreditCustomer(int customerNumber, string customerName, int amountDue, int Rate) : base(customerNumber, customerName, amountDue)
        { }
        public CreditCustomer(): this(0, "", 0, 0)
        { }
        public override string ToString()
        {
            return (GetType() + " Credit Customer " + CustomerNumber + " " + CustomerName +
              " Amount Due is " + AmountDue.ToString("C") + " Interest rate is " + Rate + " Monthly Payment is " + MonthlyPayment);
        }
        int IComparable.CompareTo(object o)
        {
            int returnVal;
            CreditCustomer temp = (CreditCustomer)o;
            if (this.CustomerNumber > temp.CustomerNumber)
                returnVal = 1;
            else
                if (this.CustomerNumber < temp.CustomerNumber)
                    returnVal = -1;
                else
                    returnVal = 0;
            return returnVal;
        }
    }

}

我遇到错误时,无法在C#中为我的父类和子类实现Icomparable: 没有实现接口成员&System; IComparable.CompareTo

我真的需要有关如何实现Icomparable接口的帮助,以便我可以在我的对象数组上使用compareTo方法。任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:1)

如果您使用的是Visual Studio并且不确定如何实现界面,只需右键单击类声明中的IComparable

class CreditCustomer : Customer, IComparable<CreditCustomer>

并选择实施界面以及两个选项中的任何一个。

CreditCustomer中实施的正确签名可以是:

public int CompareTo(CreditCustomer other)
{
}

int IComparable<CreditCustomer>.CompareTo(CreditCustomer other)
{
}

请注意,除非您将对象强制转换为IComparable<CreditCustomer>,否则第二个签名将隐藏该方法。

答案 1 :(得分:0)

正如SLaks在评论中提到的那样,实际上有两个不同的接口,名为IComparable,很容易混淆:

  1. IComparable是较旧的非通用界面,可以在object和任何其他object之间进行比较。没有类型检查 - 您的CreditCustomer可以与intList<string>或任何内容进行比较。此接口定义单个方法CompareTo(object obj)。这是你班上的方法。

  2. IComparable<T>是一个较新的通用界面。它与IComparable非常相似,但强制执行类型检查 - 它只将T与T进行比较,因此您不必编写大量样板代码,以确保有人没有尝试比较您的{{1}到CreditCustomer或其他东西。它还定义了一个方法 - Dog。这是您将类标记为实现的接口。

  3. 尽管名称相似,但就编译器而言,这两个接口是两个不同的接口。如果您没有接受CompareTo(T obj)参数的CompareTo方法,则实施CreditCustomer,这就是为什么它会给您一个错误。更改方法签名以匹配接口,或将您正在使用的接口更改为与方法签名匹配的接口。可能是前者。

答案 2 :(得分:0)

您在语法上混淆了IComparable.CompareTo方法的隐式和显式实现。您必须使用隐式实现的语法或显式实现的语法。有关详细信息,请参阅下文。

接口方法的隐式和显式实现之间的区别:

参见例如这些其他问题:

显式实现的语法:

  • 您不得指定方法的任何辅助功能修饰符(例如publicprotectedprivate等)。它将自动为私有,并且仅在将包含类型的实例强制转换为接口类型时才可见。

  • 您在方法名称前包含接口名称。

示例:

     int IComparable.CompareTo(object other) { … }
// ^     ^^^^^^^^^^^^
// |     interface name precedes method's name
// |
// no accessibility modifier 

隐式实现的语法:

  • 可能存在辅助功能修饰符。

  • 接口的名称不得包含在方法定义中。

示例:

     public int  CompareTo(object other) { … }
//   ^^^^^^     ^
//  modifier    no interface name present
//  present