【发布时间】: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<T>。 -
Order 是用“字节”参数还是“多项式”参数调用的?
标签: c# class inheritance interface