将字符串分配给类会将其重定向到属性

发布于 2024-07-27 05:22:58 字数 381 浏览 2 评论 0原文

我有一个名为 Car 的类,其属性为 Name。 现在,每次我想更改汽车名称时,我都必须编写 Car1.Name =“Porka Turbo”。 现在我想要完成的是少 5 个字符,如下所示:

Car1 = "Porka Turbo"

它应该像这样工作:如果我分配一个从 Car 类派生的类,它应该进行常规类分配,但是当我将字符串分配给我的类时,这个字符串应该这样被重定向到名为“Name”的属性。

因此,它的工作方式与以下内容相同:

Car1.Name = "Porka Turbo"

在 C# .NET 3.5 中这可能吗?如果不是,您是否知道在此特定示例中减少编写的任何其他方法?

I have a class named Car with property Name. Now every time I like to change Name of my Car I must write Car1.Name = "Porka Turbo". Now what I would like to acomplish is 5 characters less to write like this:

Car1 = "Porka Turbo"

It should work like this: if I assign a class derrived from Car class it should make regular class assignment, but when I assign string to my class than this string should be redirected to property called "Name".

So that it will work the same as:

Car1.Name = "Porka Turbo"

Is this possible in C# .NET 3.5 and if not do you know any other method to write less in this particular example?

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

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

发布评论

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

评论(5

像极了他 2024-08-03 05:22:58

假设 Car1Car 类型的变量,则不能这样做。 你也不应该——这是可读性的噩梦。

给变量赋值应该意味着变量值更改为赋值运算符右侧的(可能已转换的)值。 您正在尝试更改运算符的含义。

如果您对更具可读性中的字符数量感到困扰:

Car1.Name = "Porka Turbo";

可以创建一个单字母方法:

Car1.X("Porka Turbo");

或另一个属性:

Car1.N = "Porka Turbo";

我不会执行以下任一操作那些虽然。 保持代码简单易读。 如果您优先考虑“源代码中的最少字符数”而不是可读性,那么将来必须维护代码的人(可能是您)将不会感谢您。

编辑:尚未提供的一个选项是提供从字符串到 Car 的隐式转换。 那可以让你写

Car1 = "Porka Turbo";

,但它不会改变现有汽车的名称属性 - 它会创建一个汽车并分配Car1 的值。 我提到这一点只是为了防止其他人将其作为一个选项而没有提及它的问题:)

Assuming Car1 is a variable of type Car, you can't do it. Nor should you - it's a readability nightmare.

Assignment to a variable should mean a change in the value of the variable to the (possibly converted) value on the RHS of the assignment operator. You're trying to change the meaning of the operator.

If you're bothered by the number of characters in the far more readable:

Car1.Name = "Porka Turbo";

you could create a single-letter method:

Car1.X("Porka Turbo");

Or another property:

Car1.N = "Porka Turbo";

I wouldn't do either of those though. Keep the code simple and readable. Whoever has to maintain the code in the future (which may be you) will not thank you if you prioritise "minimal number of characters in source" over readability.

EDIT: One option which hasn't been presented yet is providing an implicit conversion from string to Car. That would let you write

Car1 = "Porka Turbo";

but it wouldn't be changing the name property of the existing car - it would be creating a new car and assigning that value to Car1. I mention this only in case someone else drops that in as an option without mentioning the problem with it :)

初与友歌 2024-08-03 05:22:58

我不知道是否有一种方法可以实现您正在寻找的东西(也许有),但我建议不要这样做,因为我会发现它令人困惑。 对于初学者来说,它会使示例中的 Car1 实例表现得好像它是一个字符串(事实并非如此),并且不太清楚代码实际执行的操作。

I don't know if there is a way of accomplishing what you are looking for (perhaps there is), but I would advice against doing so, since I would find it confusing. For starters, it would make the Car1 instance in your example to act as if it was a string (which it is not), and it will be less clear what the code actually does.

情痴 2024-08-03 05:22:58

不可以,C# 不允许重载赋值运算符。

No, C# does not allow you to overload the assignment operator.

吖咩 2024-08-03 05:22:58

我认为对 OOP 的一个非常深刻的误解是这个请求的根源。 像 C# 这样的面向对象语言的好处是 Car 类型的对象将始终是 Car,而不是其他。 字符串永远是字符串,而字符串永远不会是汽车。 将一段文本放在双引号内恰好是创建新字符串实例的简写。 没有这样的用于创建新 Car 实例的简写。 你永远不能说“这个字符串实际上是一辆汽车”,因为诸如字符串没有“Name”属性之类的原因。

我能说的最好的事情是,继续使用 C#,很快您就会意识到情况就是这样 =)

I think a very deep misunderstanding of OOP is the root of this request, in the first place. The good thing with an object oriented language like C# is that an object of the type Car will always be a Car, and nothing else. A string will always be a string, and a String will never be a Car. Wrapping a piece of text within double quotation marks happens to be shorthand for creating a new string instance. There is no such shorthand for creating a new Car instance. You can never say that "This string is actually a Car", for reasons like, say, the fact that a String doesn't have the "Name" property.

The best thing I can say is, keep working with C#, and soon enough you'll come to appreciate that this is the case =)

三生一梦 2024-08-03 05:22:58

这是不可能的,因为赋值运算符不能重载。

即使有可能,我也不应该这样做。 您从中获得什么?
您需要输入的字符少了一些吗?
请牢记这样一个事实:代码的读取次数远多于编写次数。
当你在一两年后重新阅读/审查该代码时,我想你会挠头几次……

此外,假设可以重载赋值运算符:

你会怎么做?如果您想将另一个 Car 实例分配给 Car1 该怎么办?

如果 Car1 为 NULL 你会怎么做?

It is not possible since the assignment operator cannot be overloaded.

Even it it would be possible, I shouldn't do it. What do you gain with it ?
A few less characters that you have to type ?
Keep the fact in the back of your mind that code is being read far more often than it is written.
When you re-read / review that code after a year or 2, I think you'll be scratching the back of your head a few times ...

Besides, suppose that it would be possible to overload the assignment operator:

What will you do if you want to assign another Car instance to Car1 ?

What will you do if Car1 is NULL ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文