简体   繁体   English

C# 中的重载赋值运算符

[英]Overloading assignment operator in C#

I know the = operator can't be overloaded, but there must be a way to do what I want here:我知道=运算符不能重载,但必须有一种方法可以在这里做我想做的事情:

I'm just creating classes to represent quantitative units, since I'm doing a bit of physics.我只是在创建类来表示定量单位,因为我正在做一些物理学。 Apparently I can't just inherit from a primitive, but I want my classes to behave exactly like primitives -- I just want them typed differently.显然我不能只从原语继承,但我希望我的类的行为与原语完全一样——我只是希望它们的类型不同。

So I'd be able to go,所以我可以 go,

Velocity ms = 0;
ms = 17.4;
ms += 9.8;

etc.等等

I'm not sure how to do this.我不知道该怎么做。 I figured I'd just write some classes like so:我想我会像这样写一些类:

class Power
{
    private Double Value { get; set; }

    //operator overloads for +, -, /, *, =, etc
}

But apparently I can't overload the assignment operator.但显然我不能重载赋值运算符。 Is there any way I can get this behavior?有什么办法可以得到这种行为?

It sounds like you should be using a struct rather than a class... and then creating an implicit conversion operator, as well as various operators for addition etc. 听起来你应该使用结构而不是类...然后创建一个隐式转换运算符,以及各种运算符来添加等。

Here's some sample code: 这是一些示例代码:

public struct Velocity
{
    private readonly double value;

    public Velocity(double value)
    {
        this.value = value;
    }

    public static implicit operator Velocity(double value)
    {
        return new Velocity(value);
    }

    public static Velocity operator +(Velocity first, Velocity second)
    {
        return new Velocity(first.value + second.value);
    }

    public static Velocity operator -(Velocity first, Velocity second)
    {
        return new Velocity(first.value - second.value);
    }

    // TODO: Overload == and !=, implement IEquatable<T>, override
    // Equals(object), GetHashCode and ToStrin
}

class Test
{
    static void Main()
    {
        Velocity ms = 0;
        ms = 17.4;
        // The statement below will perform a conversion of 9.8 to Velocity,
        // then call +(Velocity, Velocity)
        ms += 9.8;
    }
}

(As a side-note... I don't see how this really represents a velocity, as surely that needs a direction as well as a magnitude.) (作为旁注......我不知道这是如何真正代表速度的,因为当然需要方向和幅度。)

You can create implicit conversion operators. 您可以创建隐式转换运算符。 There is a page on MSDN with a nice example. MSDN上有一个页面,有一个很好的例子。

It's also a good idea to make them immutable structs. 使它们成为不可变结构也是一个好主意。 That's exactly what the "primitives" are, and that's what makes it impossible to inherit from them. 这正是“基元”的含义,而这就是不可能继承它们的原因。 You want a struct because you want value-type semantics, instead of reference type semantics. 您需要一个结构,因为您需要值类型语义,而不是引用类型语义。 And you want them immutable because mutable value types are generally a bad idea. 而且你希望它们不可变,因为可变值类型通常是个坏主意。

I think it cannot be overloaded because C# classes are all derived from Object, so they are basically objects, and when you use the assignment operators, you are basically just referencing another object. 我认为它不能被重载,因为C#类都是从Object派生的,所以它们基本上是对象,当你使用赋值运算符时,你基本上只是引用另一个对象。 On the other hand, if you use a structure, then you basically need all the information, so when you use = operator, all fields will be copied. 另一方面,如果使用结构,则基本上需要所有信息,因此当使用=运算符时,将复制所有字段。

So I would say, face it, and implement a function called Copy() and you should be fine :-) 所以我会说,面对它,并实现一个名为Copy()的函数,你应该没问题:-)

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

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