简体   繁体   English

覆盖对象返回值

[英]Override objects return value

I'm trying to compare an object with an int value such as 我正在尝试将对象与int值进行比较,例如

if (myObject - 5 == 0)
    doSomething();

my class could look something like this: (most setters/getters removed, so don't mind that all variables are private) 我的课可能看起来像这样:( 删除了大多数setter / getters,所以不要介意所有变量都是私有的)

public class SomeClass
{
    public string name;
    private int minValue;
    private int maxValue;
    private int currValue;

    public int getCurrentValue()
    {
        return currValue;
    }
}

What I'm trying to achieve is something like this: 我想要实现的是这样的:

someClassInstance - 5;

to be equal 平等

someClassInstance.getCurrentValue() - 5;

Can I make an override for the object to act as an int (it's own variable) opposed to just being an object? 我可以覆盖对象作为一个int(它自己的变量)而不是仅仅作为一个对象吗?

May be operator is the case? 可能是运营商是这样的吗?

public class SomeClass {
  ...

  public static int operator -(SomeClass left, int right) {
    if (Object.ReferenceEquals(null, left))
      throw new ArgumentNullException("left");

    return left.getCurrentValue() - right;
  }
}

...

SomeClass someClassInstance = new SomeClass(...);

int result = someClassInstance - 5;

Another possibility (based on implicit operator ) is to convert SomeClass implicitly to int whenever required: 另一种可能性(基于隐式运算符 )是在需要时 SomeClass隐式转换int

public class SomeClass {
  ...

  // Whenever int is requiered, but SomeClass exists make a conversion
  public static implicit operator int(SomeClass value) {
    if (Object.ReferenceEquals(null, value))
      throw new ArgumentNullException("value");

    return value.getCurrentValue();
  }
}

...

SomeClass someClassInstance = new SomeClass(...);

int result = someClassInstance - 5;

Actually you would be much better off overriding operator int , that way you can do far more calculations with less overloads: 实际上你会更好地覆盖operator int ,这样你可以用更少的重载做更多的计算:

using System;

namespace Demo
{
    public class SomeClass
    {
        public string name;
        private int minValue;
        private int maxValue;
        public int currValue;

        public int getCurrentValue()
        {
            return currValue;
        }

        public static implicit operator int(SomeClass value)
        {
            if (value == null)
                throw new ArgumentNullException("value");

            return value.currValue;
        }
    }

    internal class Program
    {
        private void run()
        {
            var test = new SomeClass {currValue = 5};

            if (test - 5 == 0)
                Console.WriteLine("It worked");

            if (test + 5 == 10)
                Console.WriteLine("This also worked");
        }

        private static void Main()
        {
            new Program().run();
        }
    }
}

You could experiment with a mixture of implicit conversions and operator overloading , but from my experience you will never make it work as seamlessly as you wish (and as you could get it to work in C++). 您可以尝试隐式转换运算符重载的混合 ,但根据我的经验,您永远不会让它像您希望的那样无缝地工作(并且您可以使它在C ++中工作)。

If I were you, I would change the getCurrentValue to a property: 如果我是你,我会将getCurrentValue更改为属性:

public int CurrentValue
{
    get {return currValue};
}

and just use someClassInstance.CurrentValue -5 并使用someClassInstance.CurrentValue -5

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

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