简体   繁体   English

如何在继承的类上使用运算符重载以匹配C#中的基本运算符

[英]How do I use operator overload on an inherited class to match the base operator in c#

I have the following operator overload code: 我有以下运算符重载代码:

public static Set operator+(Set left, Set right)
{
    Set sum=new Set(left.Capacity+right.Capacity);
    for (left.iterator_init(); !left.iterator_end(); left.iterator_next())
        sum.add_member(left.iterator_current());
    for (right.iterator_init(); !right.iterator_end(); right.iterator_next())
        sum.add_member(right.iterator_current());
    return sum;
}

Set is a class with an array of int numbers with Capcity length. Set是一个类,其中包含具有Capcity长度的int数字数组。

The function add_member inserts a number into the array. 函数add_member将一个数字插入数组。

I also have a class OrderedSet that inherits Set like this: 我还有一个类OrderedSet,它继承了Set,如下所示:

    class OrderedSet : Set

In this class I override "add_member" function to insert the numbers ascending. 在此类中,我重写了“ add_member”函数以插入数字升序。 Now I want to use the parent class (Set) operator+ overload the exact same way on OrderedSet, in a way where don't acctually have to rewrite the function again, but depend on the overload from the parent class. 现在,我想使用父类(Set)运算符+重载,与OrderedSet上的重载方式完全相同,这种方式不必再次重写函数,而要依赖于父类的重载。 So if I wanna change the way I add two Sets or OrderedSets I will have to change the operator code ONLY on Set. 因此,如果我想更改添加两个Set或OrderedSets的方式,则只能更改Set上的操作员代码。

I tried this: 我尝试了这个:

    public static OrderedSet operator+(OrderedSet left, OrderedSet right):base(left,right)
    {

    }

But with no luck, I get syntax error. 但是没有运气,我得到语法错误。 So, what am I missing? 那么,我想念什么?

The error is generated as the methods for operators are static. 由于操作员的方法是静态的,因此会生成错误。 It is not possible to use the base keyword in a static method. 不能在静态方法中使用base关键字。

As such it's not possible using the : - It would be possible to call the base class directly within the method body however. 因此,不可能使用: -但是可以直接在方法体内调用基类。

public class Foo
{
    public static int Jump(int height)
    {
        return height * 10;
    }
}

public class Bar : Foo
{
    public static int Jump(int height)
    {
        return Foo.Jump(height);
    }
}

I think the neatest way to do this is to provide a Set constructor that creates a set by merging two sets 我认为最简单的方法是提供一个Set构造函数,通过合并两个集合来创建一个集合

protected Set(Set left, Set right)
{
    this.Capacity = left.Capacity + right.Capacity;
    for (left.iterator_init(); !left.iterator_end(); left.iterator_next())
        this.add_member(left.iterator_current());
    for (right.iterator_init(); !right.iterator_end(); right.iterator_next())
        this.add_member(right.iterator_current());
}

which you can reuse in your OrderedSet class 您可以在OrderedSet类中重复使用

protected OrderedSet(OrderedSet left, OrderedSet right)
    : base(left, right)
{

}

and then reuse both in the add overrides: 然后在添加覆盖中重用两者:

public static Set operator +(Set left, Set right)
{
    return new Set(left, right);
}

public static OrderedSet operator +(OrderedSet left, OrderedSet right)
{
    return new OrderedSet(left, right);
}

That way if the shared "concatenate two sets" logic changes you only have to change it in one place, and if you want OrderedSet to use its own logic you can stop calling the base constructor. 这样,如果共享的“连接两个集合”逻辑发生更改,则只需在一个地方进行更改,并且如果希望OrderedSet使用其自己的逻辑,则可以停止调用base构造函数。

You can call the Set version, but since it returns Set you will have to convert your Set to an OrderedSet before you can return. 您可以调用Set版本,但是由于它返回Set,因此您必须先将Set转换为OrderedSet,然后才能返回。

public static OrderedSet operator+(OrderedSet left, OrderedSet right)
{
  Set mySet = (Set)left+(Set)right;
  return SoSomethingToConvertYourSetToAnOrderedSet(mySet);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM