[英]C# - implementing an interface
I have an assignment where I need to implement an interface (IOrder) within my Polynomial Class. 我有一个任务,我需要在我的多项式类中实现一个接口(IOrder)。 The purpose of the IOrder is to compare the front Node of a Polynomial with another Polynomial and return a boolean if one is <= to the other.
IOrder的目的是将多项式的前节点与另一个多项式进行比较,如果一个<=到另一个,则返回一个布尔值。
Here is the initialization of the IOrder interface: 这是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);
}
Here are the basics of my Polynomial Class: 以下是我的多项式类的基础知识:
//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;
}
//////////////////////////////////
The problem I'm having is with the implementation of the Order interface within the Polynomial Class. 我遇到的问题是在多项式类中实现Order接口。 Just to clarify, each Polynomial has a front Node, and a Node is a Term(Coefficient double, Exponent byte) and also a Node of type next which is used in linking the Terms of a Polynomial.
为了澄清,每个多项式都有一个前节点,一个节点是一个Term(Coefficient double,Exponent byte),还有一个next类型的Node,用于链接多项式的Term。 The Polynomials are then added into a Polynomials object.
然后将多项式添加到多项式对象中。 The IOrder is to be used to sort the Polynomials in the list according to the value of the exponent of the front Term.
IOrder用于根据前项的指数值对列表中的多项式进行排序。 I think i have to downcast the Object obj in the method in order set it up so that I can compare the Exponent of ".this" Polynomial with the Exponent value of the Polynomial supplied to the method.
我想我必须在方法中向下转换Object obj以便设置它,以便我可以将“.this”多项式的Exponent与提供给方法的Polynomial的Exponent值进行比较。
Any insight into casting these values correctly would be awesome. 正确地投射这些值的任何见解都会很棒。 Thanks in advance.
提前致谢。
Is there a reason that your Order
function needs to take an object
? 您的
Order
函数是否需要获取object
? If you're always expecting a byte
to be passed into the function (and you don't want to support anything but a byte
), then you should make the argument a byte
instead. 如果你总是希望将一个
byte
传递给函数(并且你不想支持除byte
任何东西),那么你应该将参数改为一个byte
。
To answer your specific question, the is
operator checks the type of a value; 为了回答您的具体问题,
is
运算符检查值的类型; it does not perform any casting. 它不执行任何铸造。 You'd need to cast it like this:
你需要像这样投射它:
if (this.front.Item.Exponent <= (byte)obj)
{
return true;
}
But if you were to follow the above advice, you would have a function definition in your interface that looks like this: 但是如果你要遵循上面的建议,你的界面中会有一个如下所示的函数定义:
bool Order(byte exponent);
(Note that I named it exponent
. Give your parameters and variables meaningful names rather than things like "obj") (注意,我将其命名为
exponent
。为参数和变量赋予有意义的名称,而不是像“obj”这样的东西)
Then implement it like this: 然后像这样实现它:
public bool Order(byte exponent)
{
if (this.front.Item.Exponent <= exponent)
{
return true;
}
else
{
return false;
}
}
If you want, you can simply the code a bit more by removing the whole if
block. 如果需要,可以通过删除整个
if
块来简单地编写代码。 Since the expression inside the if
must evaluate to a boolean and that's what your function returns, then you should be able to reduce the whole body of the function to a single statement. 由于
if
的表达式必须求值为布尔值,并且这是函数返回的值,因此您应该能够将函数的整个主体简化为单个语句。
Why not: 为什么不:
public bool Order(Object obj)
{
if ( (obj is byte) && (this.front.Item.Exponent <= (byte) obj) )
{
return(true);
}
return(false);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.