如何在静态类中重写 ToString?
我有一个公共静态类,我想在其中有一个 ToString() 方法。
我已将其定义为 public static string ToString(),但收到以下警告:
“Class.ToString()”隐藏继承成员“object.ToString()”。 要使当前成员覆盖该实现,请添加 override 关键字。 否则添加新关键字。
如果我添加 override 关键字,则会收到此错误:
静态成员“Class.ToString()”不能标记为重写、虚拟或抽象
如何消除该警告并让我的静态类具有 ToString() 方法。
谢谢你,
基思
I have a public static class in which I would like to have a ToString() method.
I have defined it as public static string ToString(), but get the following warning:
'Class.ToString()' hides inherited member 'object.ToString()'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.
If I add the override keyword I get this error instead:
A static member 'Class.ToString()' cannot be marked as override, virtual, or abstract
How do I get rid of that warning and let my static class have the ToString() method.
Thank you,
Keith
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,使用“new”修饰符将有效地消除编译器警告,但您显式隐藏具有静态方法的实例方法。 (这与重写方法不同。)通常,除非有充分的理由,否则您不想隐藏实例方法,并且您确实不应该使用静态方法隐藏它,因为这确实会改变调用的行为语义。 .NET 中的每个对象都有一个名为 ToString() 的实例方法,该方法具有开发人员期望的特定行为; 通过使用新的静态方法隐藏该行为,您正在改变这种期望,这可能会导致很多混乱。
你“串线”是什么? 静态类通常不保存内部状态,因此实际上不应该提供任何内部数据作为 ToString() 调用的逻辑输出。 您可能需要重新考虑您的类设计或提供不同的方法名称,以更清楚地指示该方法的用途,而不隐藏实例 ToString()。
Yes, using the "new" modifier will effectively silence the compiler warning but you are explicitly hiding an instance method with a static method. (This is different than overriding the method.) Typically you don't want to hide an instance method except with very good reasons and you really shouldn't hide it with a static method as that really changes the behavior semantics of the call. Every object in .NET has an instance method named ToString() that has specific behavior that developers expect; by hiding that behavior with a new static method you are changing that expectation which can lead to a lot of confusion.
What are you "to stringing"? Static classes typically don't hold internal state so there really shouldn't be any internal data to provide as the logical output of a ToString() call. You may want to rethink your class design or provide a different method name that more clearly indicates the purpose of the method without hiding the instance ToString().
在静态类中,您不能重写 ToString。 .ToString 是一个实例方法,根据定义静态类只能有静态成员。
另外为什么要重写 .ToString() ? 无法获取该类的实例,因此无法调用该函数。
注意:使用新语法不会覆盖.ToString。 它将创建一个与 Object.ToString() 方法完全无关的新成员。
In a static class you cannot override ToString. .ToString is an instance method and by definition a static class can only have static members.
Also why would you want to override .ToString()? There is no way to get an instance of the class and hence no way to call the function.
Note: Using the new syntax will not override .ToString. It will create a new member that is completely unrelated to the Object.ToString() method.
好吧,在提出问题时,我找到了答案:
新的修饰符:
现在的方法是:
谢谢,
基思
Ok, so in asking the question, I found an answer:
The new Modifier:
here is the method now:
Thank you,
Keith