【问题标题】:C# - implementing an interfaceC# - 实现一个接口
【发布时间】:2011-10-03 17:38:59
【问题描述】:

我有一个任务需要在我的多项式类中实现一个接口 (IOrder)。 IOrder 的目的是将多项式的前节点与另一个多项式进行比较,如果一个

这里是IOrder接口的初始化:

 //Definition for IOrder Interface
        public interface IOrder
        {
            // Declare an interface that takes in an object and returns a boolean value
            // This method will be used to compare two objects and determine if the degree (exponent) of one is <= to the other.
            bool Order(Object obj);

        }

这是我的多项式类的基础知识:

        //Definition for Polynomial Class
        public class Polynomial : IOrder
        {
            //this class will be used to create a Polynomial, using the Term and Node objects defined previously within this application
            //////////////////////////////////
            //Class Data Members/
            //////////////////////////////////
            private Node<Term> front;  //this object will be used to represent the front of the Polynomial(Linked List of Terms/Mononomials) - (used later in the application)

            //////////////////////////////////
            //Implemention of the Interfaces
            //////////////////////////////////
            public bool Order(Object obj) //: IOrder where obj : Polynomial 
            {
                // I know i was so close to getting this implemented propertly
                // For some reason the application did not want me to downcast the Object into a byte

               // //This method will compare two Polynomials by their front Term Exponent
               // //It will return true if .this is less or equal to the given Polynomial's front node.
               if (this.front.Item.Exponent <= obj is byte)
               {
                   return true;
               }

            }
            //////////////////////////////////
            //Class Constructor
            //////////////////////////////////   
            public Polynomial()
            {
                //set the front Node of the Polynomial to null by default
                front = null;
            }
            //////////////////////////////////

我遇到的问题是多项式类中 Order 接口的实现。澄清一下,每个多项式都有一个前节点,一个节点是一个项(系数双精度,指数字节),也是一个类型为 next 的节点,用于连接多项式的项。然后将多项式添加到多项式对象中。 IOrder 用于根据前面 Term 的指数值对列表中的多项式进行排序。我想我必须在方法中向下转换 Object obj 才能对其进行设置,以便我可以将“.this”多项式的指数与提供给该方法的多项式的指数值进行比较。

任何关于正确转换这些值的见解都会很棒。 提前致谢。

【问题讨论】:

  • 那么为什么不将obj 转换为Polynomial?它不是一个字节,而是一个多项式,至少如果你的评论说的是实话的话。
  • 您应该告诉任何设置分配的人,他们应该真正使用标准的 .NET 类型:IComparable 及其通用等价物IComparable&lt;T&gt;
  • Order 是用“字节”参数还是“多项式”参数调用的?

标签: c# class inheritance interface


【解决方案1】:

您的Order 函数是否需要使用object?如果您总是希望将 byte 传递给函数(并且您不想支持除 byte 之外的任何内容),那么您应该将参数设为 byte

为了回答您的具体问题,is 运算符会检查值的类型;它不执行任何铸造。你需要像这样投射它:

if (this.front.Item.Exponent <= (byte)obj)
{
    return true;
}

但如果您遵循上述建议,您的界面中的函数定义将如下所示:

bool Order(byte exponent);

(请注意,我将其命名为 exponent。为您的参数和变量提供有意义的名称,而不是像“obj”之类的名称)

然后像这样实现它:

public bool Order(byte exponent)
{
    if (this.front.Item.Exponent <= exponent)
    {
        return true;
    }
    else
    {
        return false;
    }
}

如果需要,您可以通过删除整个 if 块来简化代码。由于if 中的表达式必须计算为布尔值,这就是您的函数返回的值,因此您应该能够将整个函数体简化为一条语句。

【讨论】:

  • 可能使用了 Object,因为该接口可能由一个使用字节以外的东西作为排序标准的类实现
  • @Jim Rhodes 不是一个很好的界面设计,因为它迫使实现者进行类型检查......
  • @djacobson 这不就是 IComparable.CompareTo(Object obj) 的工作原理吗?
  • @Jim Rhodes 查看 MSDN 文档...看来您是对的,他们的示例检查类型并在类型错误时抛出。猜猜这就是泛型的用途:我更喜欢实现IComparable&lt;T&gt;。 :)
  • @JimRhodes:那是因为IComparable 在引入泛型(.NET 2.0)之前是 BCL 的一部分,并且还存在(如您所指出的)比较不同对象的可能性类型。 djacobson 是正确的,因为它不是一个好的设计,但在这种特殊情况下,它是 only 真正的设计选择。鉴于 OP 的问题(以及听起来像是学校作业的事实),我怀疑在这种特殊情况下是否需要同样的灵活性。
【解决方案2】:

为什么不:

public bool Order(Object obj)
{
    if ( (obj is byte) && (this.front.Item.Exponent <= (byte) obj) )
    {
        return(true);
    }
    return(false);
}

【讨论】:

    猜你喜欢
    • 2014-01-18
    • 1970-01-01
    • 2012-12-15
    • 2018-07-03
    • 2020-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多