C# - 实现接口

发布于 2024-12-08 00:01:16 字数 2443 浏览 0 评论 0原文

我有一个作业,需要在多项式类中实现一个接口(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);

        }

以下是我的 Polynomial 类的基础知识:

        //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;
            }
            //////////////////////////////////

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

玩物 2024-12-15 00:01:16

您的 Order 函数需要采用 对象 是否有原因?如果您总是期望将 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 内的表达式必须计算为布尔值,并且这就是函数返回的值,因此您应该能够将函数的整个主体简化为单个语句。

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.

翻身的咸鱼 2024-12-15 00:01:16

为什么不:

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

Why not:

public bool Order(Object obj)
{
    if ( (obj is byte) && (this.front.Item.Exponent <= (byte) obj) )
    {
        return(true);
    }
    return(false);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文