字符串方法

发布于 2024-12-08 01:55:17 字数 500 浏览 0 评论 0原文

您好,我有一个班级表格和班级特定表格。 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 技术交流群。

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

发布评论

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

评论(4

比忠 2024-12-15 01:55:17

我不确定我是否理解这个问题,但如果我答对了,您会想知道为什么 Form 类型的实例的行为与 ExtendedForm 类型的实例不同。

重写仅替换“子”类型对象上的方法,而不替换“父”类型对象上的方法。首先,这不是继承应该如何工作的,但实际上也是不可能的,即如果您有两个扩展类怎么办?应该使用哪个 ToString 覆盖?

下面应该解释它是如何工作的。

 public class ClassBase
 {
      public override string ToString()
      {
           return "base";
      }
 }

 public class ClassA : ClassBase
 {
      public override string ToString()
      {
           return "A";
      }
 } 

 public class ClassB : ClassBase
 {
      public override string ToString()
      {
           return "B";
      }
 } 

 public class ClassC : ClassA
 {
      // No override
 }

这将产生以下内容

 ClassBase baseClass = new ClassBase();
 baseClass.ToString() // Returns "base"

 ClassA aClass = new ClassA();
 aClass.ToString(); // Returns "A"

 ClassB bClass = new ClassB();
 bClass.ToString(); // returns "B"

 ClassC cClass = new ClassC();
 cClass.ToString(); // returns "A" because no override exists

请记住,您拥有哪种类型的参考并不重要。假设你有一个对 ClassBase 的引用,它实际上引用了一个 ClassA,那么 ClassA 重写仍然会被调用

  ClassBase baseClass = new ClassA();
  baseClass.ToString(); // Returns "A" because baseClass is actually a 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.

 public class ClassBase
 {
      public override string ToString()
      {
           return "base";
      }
 }

 public class ClassA : ClassBase
 {
      public override string ToString()
      {
           return "A";
      }
 } 

 public class ClassB : ClassBase
 {
      public override string ToString()
      {
           return "B";
      }
 } 

 public class ClassC : ClassA
 {
      // No override
 }

This would produce the following

 ClassBase baseClass = new ClassBase();
 baseClass.ToString() // Returns "base"

 ClassA aClass = new ClassA();
 aClass.ToString(); // Returns "A"

 ClassB bClass = new ClassB();
 bClass.ToString(); // returns "B"

 ClassC cClass = new ClassC();
 cClass.ToString(); // returns "A" because no override exists

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

  ClassBase baseClass = new ClassA();
  baseClass.ToString(); // Returns "A" because baseClass is actually a ClassA
相守太难 2024-12-15 01:55:17

您看不到新方法,因为 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?

愁以何悠 2024-12-15 01:55:17

您不清楚问题是什么,代码示例总是有帮助的,但我将介绍我能想到的两个:

Form form = new Form();
form.ToString(); //returns FormProxy923a5e0e9d7a46b7baa4dfe2173af18c

这是可以预料的,因为这里不涉及 SpecificForm。

Form form = new SpecificForm();
form.ToString(); //returns FormProxy923a5e0e9d7a46b7baa4dfe2173af18c

这可能是由于 ToString() 定义中缺少 override 关键字造成的,它应该如下所示:

public override string ToString()
{
    //Do stuff
}

如果您缺少 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:

Form form = new Form();
form.ToString(); //returns FormProxy923a5e0e9d7a46b7baa4dfe2173af18c

This is to be expected, as SpecificForm is not involved here.

Form form = new SpecificForm();
form.ToString(); //returns FormProxy923a5e0e9d7a46b7baa4dfe2173af18c

This is likely caused by missing the override keyword in your ToString() definition, it should look like this:

public override string ToString()
{
    //Do stuff
}

If you are missing the override the default behavior is to create a non-virtual method, which only affects variables of type SpecificForm in your example.

残龙傲雪 2024-12-15 01:55:17

不确定我是否理解你的问题。如果您问为什么此部分:

return this.Description;

不执行而不是此部分:

if (this.Description == null)
return this.Form.ToString();

我只能说,在执行此部分之前,您的编码路径中未设置“this.Description”。

Not sure if I understood your question. If you are asking why this part:

return this.Description;

doesn't execute instead of this part:

if (this.Description == null)
return this.Form.ToString();

I can only say, "this.Description" was not set in your coding path before this part gets executed.

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