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

发布于 2024-11-09 19:34:35 字数 1794 浏览 0 评论 0原文

我正在用 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 类中,this 是一个 Int32 类型的二维数组(Double in a更新、更高级的版本);它是 ComplexMatrix 中的 System.Numerics.Complex 类型
  • this.Rows, this.Columns 属性:矩阵的行数和列数

我有几个问题:

  • 当在 ComplexMatrix 实例上调用 ToString 时,并调用基本 ToString () 方法,是否尝试进行从 Complex 到 Int32 的类型转换?
  • 由于 ComplexMatrix Elements 属性(Complex[,] 类型)隐藏了基类 Elements 属性(Int32[,] 类型),因此需要 new 关键字吗?
  • “this”kwyword 是否被视为矩阵类型而不是 ComplexMatrix?

I am writing a class library in C# for working with matrices, and am currently working on a subclass of Matrix called ComplexMatrix. The Matrix base class works with values of the Int32 data type (a more advanced version uses Double), and the ComplexMatrix of the System.Numerics.Complex structure (.NET 4).

For the base class, I overrode ToString() as:

| 1 2 |
| 3 4 |    printed as    {{1,2}{3,4}}

The System.Numerics.Complex structure overrides ToString() in the form:

a+bi    printed as    (a,b)    where a is real and b is imaginary

When overriding ToString in ComplexMatrix, I simply used the method:

public override string ToString()
{
    return base.ToString();
}

Unfortunately, for a complex matrix, the following occurred:

| 1+1i 1+2i |
| 2+1i 2+2i |   printed as   {{0,0}{0,0}} rather than {{(1,1),(1,2)}{(2,1)(2,2)}}

The original ToString() code I wrote for the Matrix class is:

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();
}

In the above code:

  • this.Elements Property: in the Matrix class, this is a 2-dimensional array of Int32 type (Double in a newer, more advanced version); it is of System.Numerics.Complex type in ComplexMatrix
  • this.Rows, this.Columns Properties: the number of rows and columns respectively of the matrix

Several questions I have are:

  • When ToString is called on a ComplexMatrix instance, and calls the base ToString() method, is an attempted type conversion taking place from Complex to Int32?
  • As the ComplexMatrix Elements property (Complex[,] type) is hiding the base class Elements property (Int32[,] type), the new keyword is required?
  • Is the "this" kwyword being seen as a Matrix type rather than ComplexMatrix?

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

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

发布评论

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

评论(2

挽容 2024-11-16 19:34:35

我认为您的问题如下:
Elements 属性在 Matrix 类中未标记为 virtualComplexMatrix 类中的 Elements 属性隐藏了 Matrix 类的 Elements 属性。因此,多态性不起作用,并且 Matrix 中的 ToString 方法访问 Matrix.Elements 而不是 ComplexMatrix.Elements。但由于 Elements 是一个属性,并且您想要更改该属性的类型,因此无论如何都不能使用 virtual

要解决此问题,您应该执行以下操作:

  • 创建一个通用基类,该基类希望将矩阵值的类型作为通用参数
  • 创建一个从该基类继承并传递 SimpleMatrix 类int 作为通用参数。
  • 创建一个从该基类继承的 ComplexMatrix 类,并将 Complex 作为泛型参数传递。

I think your problem is the following:
The Elements property is not marked virtual in the Matrix class. The Elements property in the ComplexMatrix class hides the Elements property of the Matrix class. Therefore polymorphism isn't working and the ToString method in Matrix accesses Matrix.Elements and not ComplexMatrix.Elements. But because Elements is a property and you want to change the type of the property, you can't use virtual anyway.

To fix the problem, you should do something like this:

  • Create a generic base class that wants the type of the matrix values as generic parameter
  • Create a SimpleMatrix class that inherits from this base class and passes int as generic parameter.
  • Create a ComplexMatrix class that inherits from this base class and passes Complex as generic parameter.
梦纸 2024-11-16 19:34:35

你无法真正做你想做的事。

问题是 C# 中没有适用于所有数字类型的超类。因此,您不能真正在像这样的类层次结构中用一种数字类型替换另一种数字类型(并且仍然对它们进行数学计算)。

您可以做的是创建一个 IMatrixElement 接口。然后创建实现该接口的具体类 MatrixElementInt、MatrixElementDouble、MatrixElementComplex。然后在构建矩阵时提供正确的元素类型。

interface IMatrixElement
{
    IMatrixElement Add(IMatrixElement a, IMatrixElement b);
    IMatrixElement Multiply(IMatrixElement a, IMatrixElement b);
    string ToString();
}

You can't really do what you are trying to do.

The problem is that there is no superclass for all of the numeric types in C#. So you can't really substitute one numeric type for another in a class heirarchy like this (and still do math on them).

What you could do is create an IMatrixElement interface. Then create the concrete classes MatrixElementInt, MatrixElementDouble, MatrixElementComplex that implement that interface. Then supply the correct element type when constructing your matrix.

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