从基类继承 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 类中,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您的问题如下:
Elements
属性在Matrix
类中未标记为virtual
。ComplexMatrix
类中的Elements
属性隐藏了Matrix
类的Elements
属性。因此,多态性不起作用,并且Matrix
中的ToString
方法访问Matrix.Elements
而不是ComplexMatrix.Elements
。但由于Elements
是一个属性,并且您想要更改该属性的类型,因此无论如何都不能使用virtual
。要解决此问题,您应该执行以下操作:
的
作为通用参数。SimpleMatrix
类intComplexMatrix
类,并将Complex
作为泛型参数传递。I think your problem is the following:
The
Elements
property is not markedvirtual
in theMatrix
class. TheElements
property in theComplexMatrix
class hides theElements
property of theMatrix
class. Therefore polymorphism isn't working and theToString
method inMatrix
accessesMatrix.Elements
and notComplexMatrix.Elements
. But becauseElements
is a property and you want to change the type of the property, you can't usevirtual
anyway.To fix the problem, you should do something like this:
SimpleMatrix
class that inherits from this base class and passesint
as generic parameter.ComplexMatrix
class that inherits from this base class and passesComplex
as generic parameter.你无法真正做你想做的事。
问题是 C# 中没有适用于所有数字类型的超类。因此,您不能真正在像这样的类层次结构中用一种数字类型替换另一种数字类型(并且仍然对它们进行数学计算)。
您可以做的是创建一个 IMatrixElement 接口。然后创建实现该接口的具体类 MatrixElementInt、MatrixElementDouble、MatrixElementComplex。然后在构建矩阵时提供正确的元素类型。
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.