从基类继承ToString时,数据类型混乱

问题描述

|| 我正在用C#语言编写一个用于处理矩阵的类库,并且目前正在处理名为ComplexMatrix的Matrix子类。 Matrix基类使用Int32数据类型(更高级的版本使用Double)和System.Numerics.Complex结构(.NET 4)的ComplexMatrix的值。 对于基类,我将ToString()重写为:
| 1 2 |
| 3 4 |    printed as    {{1,2}{3,4}}
System.Numerics.Complex结构以以下形式覆盖ToString():
a+bi    printed as    (a,b)    where a is real and b is imaginary
当在ComplexMatrix中覆盖ToString时,我只是使用了以下方法:
public override string ToString()
{
    return base.ToString();
}
不幸的是,对于一个复杂的矩阵,发生了以下情况:
| 1+1i 1+2i |
| 2+1i 2+2i |   printed as   {{0,0}{0,0}} rather than {{(1,1),(1,2)}{(2,1)(2,2)}}
我为Matrix类编写的原始ToString()代码是:
public override string ToString()
{
    StringBuilder matrixString = new StringBuilder();
    string comma = \"\";

    matrixString.Append(\"{\");
    for (int i = 0; i < this.Rows; i++)
    {
        matrixString.Append(\"{\");
        for (int j = 0; j < this.Columns; j++)
        {
            if (j == 0) comma = \"\";
            else comma = \",\";

            matrixString.Append(comma + this.Elements[i,j].ToString());
        }
        matrixString.Append(\"}\");
    }
    matrixString.Append(\"}\");
    return matrixString.ToString();
}
在上面的代码中: this.Elements属性:在Matrix类中,这是Int32类型的二维数组(在较新的,更高级的版本中为Double);它是ComplexMatrix中的System.Numerics.Complex类型 this.Rows,this.Columns属性:矩阵的行数和列数 我有几个问题: 当在ComplexMatrix实例上调用ToString并调用基本的ToString()方法时,是否尝试将类型从Complex转换为Int32? 当ComplexMatrix Elements属性(Complex [,]类型)隐藏基类Elements属性(Int32 [,]类型)时,需要new关键字吗? 是将“ this”这个kwyword视为Matrix类型而不是ComplexMatrix吗?     

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)