I have an assignment where I need to implement an interface (IOrder) within my Polynomial Class. 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.
Here is the initialization of the IOrder interface:
//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. 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. 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. 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.
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
? 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.
To answer your specific question, the is
operator checks the type of a value; 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")
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. 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.
Why not:
public bool Order(Object obj)
{
if ( (obj is byte) && (this.front.Item.Exponent <= (byte) obj) )
{
return(true);
}
return(false);
}
精彩评论