字符串方法
您好,我有一个班级表格和班级特定表格。 SpecificForm 类继承 Form。特定的 Form 有一个重写 ToString 方法,可以写入有意义的字符串。但是,当我调用 ToString 或类 Form 的对象时,我得到以下输出:
FormProxy923a5e0e9d7a46b7baa4dfe2173af18c
任何想法为什么我无法获取自定义字符串,即使我覆盖它。
非常感谢任何想法和建议!
/// <summary>
/// Override of tostring
/// </summary>
public override string ToString()
{
if (this.Description == null)
return this.Form.ToString();
return this.Description;
}
Hi I have an Class Form and Class SpecificForm. Class SpecificForm inherirts Form. Specific Form has an override ToString method that writes a string that makes a sense. However when I call the ToString or object of class Form I get the following output:
FormProxy923a5e0e9d7a46b7baa4dfe2173af18c
Any ideas why I am not able to get the custom string even though I am overriding it.
Any ideas and suggestions are greatly appreciated!
/// <summary>
/// Override of tostring
/// </summary>
public override string ToString()
{
if (this.Description == null)
return this.Form.ToString();
return this.Description;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不确定我是否理解这个问题,但如果我答对了,您会想知道为什么 Form 类型的实例的行为与 ExtendedForm 类型的实例不同。
重写仅替换“子”类型对象上的方法,而不替换“父”类型对象上的方法。首先,这不是继承应该如何工作的,但实际上也是不可能的,即如果您有两个扩展类怎么办?应该使用哪个 ToString 覆盖?
下面应该解释它是如何工作的。
这将产生以下内容
请记住,您拥有哪种类型的参考并不重要。假设你有一个对 ClassBase 的引用,它实际上引用了一个 ClassA,那么 ClassA 重写仍然会被调用
I am not sure if I understand the question, but if I got it right, you are wondering why instances of type Form does not behave like instances of type ExtendedForm.
Overrides only replace the methods on objects that are of 'child' types, not the methods on the 'parents'. First of all, it's not how inheritance is supposed to work, but it's also not practically possible, i.e. what if you have two extended classes; which ToString override should be used?
The following should explain how it works.
This would produce the following
Keep in mind that it does not matter what kind of reference you have. Say you have a reference to a ClassBase, which is actually referencing a ClassA, then the ClassA override would still be called
您看不到新方法,因为 Form 类不是 SpecificForm 类。继承仅向一个方向移动。如果您的基类型也具有子类型的属性,那么当两个子类型都添加具有相同名称的成员时会发生什么?
You don't see your new method because the Form class is not a SpecificForm class. Inheritance only moves in one direction. If your base type also took on the attributes of the child type, what would happen when two child types both add members with the same name?
您不清楚问题是什么,代码示例总是有帮助的,但我将介绍我能想到的两个:
这是可以预料的,因为这里不涉及 SpecificForm。
这可能是由于 ToString() 定义中缺少
override
关键字造成的,它应该如下所示:如果您缺少
override
,则默认行为是创建一个非-虚拟方法,仅影响示例中SpecificForm
类型的变量。You aren't being clear on what the problem is, code examples always help, but I will cover the two I can think of:
This is to be expected, as SpecificForm is not involved here.
This is likely caused by missing the
override
keyword in your ToString() definition, it should look like this:If you are missing the
override
the default behavior is to create a non-virtual method, which only affects variables of typeSpecificForm
in your example.不确定我是否理解你的问题。如果您问为什么此部分:
不执行而不是此部分:
我只能说,在执行此部分之前,您的编码路径中未设置“this.Description”。
Not sure if I understood your question. If you are asking why this part:
doesn't execute instead of this part:
I can only say, "this.Description" was not set in your coding path before this part gets executed.