简体   繁体   English

替代赋值运算符重载

[英]Alternative to assignment operator overloading

Let me start off by stating that I know the assignment operator in C# cannot be overloaded/overridden. 让我首先说明我知道C#中的赋值运算符不能被重载/重写。 However, if I have a class like so: 但是,如果我有这样的课:

public class Time
{

    private int Hour, Minute;
    public int minutes;

    public Time(int m)
    {
        this.minutes = m;
        this.Hour = m / 60;
        this.Minute = m % 60;
    }

    public Time(int hh, int mm)
    {
        this.minutes = 60 * hh + mm;
        this.Hour = hh;
        this.Minute = mm;
    } 

    public static explicit operator Time(int i1)
    {
        return new Time(i1);
    } 

}

Two things: 两件事情:

1) Is the explicit conversion overload at the bottom necessary? 1)底部的显式转换过载是否必要?

2) I want to do 2)我想做

Time newTime = 510;

and have Hour and Minute reflect the new minutes . 并有小时分钟反映新的分钟 Without making another object, is the only way to make a function like so: 没有制作另一个对象,是制作这样一个函数的唯一方法:

    public void changeminutes(int m)
    {
        this.minutes = m;
        this.Hour = m / 60;
        this.Minute = m % 60;
    }

and do this: 这样做:

Time newTime = new Time();
newTime.changeminutes(510);

edit: 编辑:

I probably should have mentioned that I'm still in learning C#. 我可能应该提到我还在学习C#。 A lot of your suggestions are flying over my head. 很多你的建议都飞过我的脑海。 Sorry. 抱歉。

next edit : This isn't necessarily about time. 下一个编辑:这不一定是时间。 I just used it to illustrate my example. 我只是用它来说明我的例子。

You can create an implicit cast from int to Time . 您可以创建从intTime的隐式转换。

However, I recommend against it. 但是,我建议不要这样做。
Unless they're between equivalent types (eg, int and long ), implicit casts will end up creating lots of confusion. 除非它们介于等效类型之间(例如, intlong ),否则隐式强制转换最终会造成很多混乱。
In addition, it isn't obvious that that should mean minutes and not seconds. 此外,这不应该是指分钟而不是秒。

You can create a much nicer syntax using extension methods: 您可以使用扩展方法创建更好的语法:

Time t = 510.Minutes();

It looks like you're trying to make a reference type behave like a value type, which is... um, tricky. 看起来你正试图让引用类型表现得像一个值类型,这是......呃,很棘手。 If you really don't want to use the constructor for this (and explaining why that is might help us answer), you can use a static "factory" method: 如果您真的不想使用构造函数(并解释为什么这可能有助于我们回答),您可以使用静态“工厂”方法:

public static Time FromMinutes(int m)
{
  return new Time(m);
}

And use it like this: 并像这样使用它:

var t = Time.FromMinutes(510);

This is similar to what the TimeSpan class lets you do. 这类似于TimeSpan类允许您执行的操作。

For you case an implicit conversion would work but... 对于你的情况,隐式转换可行,但......

Conversions are not to be used when you loose data, or even semantics like in this case. 丢失数据时甚至不使用转换,甚至在这种情况下也不会使用语义。 You know that the number will become a Time, but you don't know which unit of time will the number be converted into. 您知道该号码将成为时间,但您不知道该号码将转换为哪个时间单位。

If somebody is going to read the code it will not have any clue what the number represents without looking at the internal definition of the Time class. 如果有人要阅读代码,那么在不查看Time类的内部定义的情况下,它将不知道数字代表什么。 Think if that was in an outside project. 想想那是否属于外部项目。 Debugging hell? 调试地狱? :) :)

First, you want to use implicit casting to achieve Time x = 123; 首先,你想使用implicit转换来实现Time x = 123; , but as others are pointing out it's probably a poor design choice because it's not immediately clear what the number represents (and most people would assume a Unix timestamp). ,但正如其他人指出它可能是一个糟糕的设计选择,因为它不能立即清楚数字代表什么(大多数人会假设一个Unix时间戳)。

Second, why not use the Minutes property to set minutes, and simply convert the value into the proper value, while changing Hours? 其次,为什么不使用Minutes属性来设置分钟,只需将值转换为适当的值,同时更改小时数?

time.Minutes = 510;

// in class def:

public int Minutes
{
    get { return minutes; }
    set
    {
        hours = value / 60;
        minutes = value % 60;
    }
}

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

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