Math.net 如何创建通用的 ToString() 覆盖
Math.net how to create generic ToString() override
Math.net 有 ToString()
大多数数据收集的实现。当在 classes 中使用时,这些需要被覆盖。我知道如何对一个变量执行此操作,但如何使其对同一类型的所有变量通用?
我的 class 定义有一个变量 ToString()
覆盖:
public class Network
{
public Matrix<double> Win { get; set; } // network input matrix
public Matrix<double> Wres { get; set; } // network reservoir matrix
public Matrix<double> Wout { get; set; } // network output matrix
// constructor
public Network(Matrix<double> Winput, Matrix<double> Wreservoir, Matrix<double> Woutput)
{
Win = Winput;
Wres = Wreservoir;
Wout = Woutput;
}
public override string ToString()
{
return Win.ToString();
}
}
这可以在 Win
上使用 Console.WriteLine(network.Win.ToString());
之类的调用,但是如何输出其他矩阵 Wres
、Wout
(具有不同的维度)?我曾尝试创建三个覆盖,但由于编译器的抱怨,这不起作用:
already defines a member called 'ToString' with the same parameter
types
此外,我相信一定有更通用、更优雅的方法来做到这一点。
您创建的 ToString()
方法适用于 Network
class...
换句话说,您已将调用 Network.ToString()
设为字符串化的 Win 矩阵。当然,您不能为 Network.ToString()
设置三种不同的行为 - 那么您希望它做什么?
我认为你的问题试图覆盖网络上所有矩阵字段network.matrix.ToString()
的行为class...在这种情况下,它是有必要覆盖 Matrix class 本身的行为,为此,您需要制作 Win
、Wres
和 Wout
subclass of Matrix<double>
用他们自己的 ToString()
覆盖:
public class Network
{
public NetworkMatrix Win { get; set; } // network input matrix
public NetworkMatrix Wres { get; set; } // network reservoir matrix
public NetworkMatrix Wout { get; set; } // network output matrix
// constructor
public Network(NetworkMatrix Winput, NetworkMatrix Wreservoir, NetworkMatrix Woutput)
{
Win = Winput;
Wres = Wreservoir;
Wout = Woutput;
}
...
}
public class NetworkMatrix : Matrix<double>
{
public override string ToString()
{
return "Overwridden tostring method.";
}
}
根据您提供构造函数的方式,这可能无法发送您的派生类型。另外,我还没有将 NetworkMatrix
设为通用,但如果您愿意,可以设为 NetworkMatrix<T> : Matrix<T>
。
说了这么多 - 我应该指出,调用类似 network.Win.ToString()
的东西通常是糟糕的编码实践 - 其他 class 不应该了解网络的内部结构,而你 运行 成问题。查看得墨忒耳法则(例如 https://en.wikipedia.org/wiki/Law_of_Demeter )以获取更多信息。 (特别值得一问的是 Win
、Wres
甚至 Wout
是否真的应该首先是 public?)
最好的方法是使用您对 Network.ToString()
方法的覆盖以合理的方式组合 Win、Wres 或 Wout 上的数据。或者创建一个矩阵字符串化器 class,它可以按照您想要的方式格式化字符串。
希望对您有所帮助。有什么问题,尽管问。
您已经在 Network
class 中将每个 Matrix
创建为 public 属性,因此您可以在需要时轻松访问它们到.
Network network = new Network(mInput, mResevoir, mOutput);
Console.WriteLine(network.Win);
Console.WriteLine(network.Wres);
Console.WriteLine(network.Wout);
编辑:我刚刚意识到属性也有一个 public 设置方法。如果您不希望在创建网络变量后更改属性,可以将设置器修改为私有,这样只能从 class.
中设置值
public class Network
{
public Matrix<double> Win { get; private set; } // network input matrix
public Matrix<double> Wres { get; private set; } // network reservoir matrix
public Matrix<double> Wout { get; private set; } // network output matrix
// constructor
public Network(Matrix<double> Winput, Matrix<double> Wreservoir, Matrix<double> Woutput)
{
Win = Winput;
Wres = Wreservoir;
Wout = Woutput;
}
public override string ToString()
{
return Win.ToString();
}
}
Math.net 有 ToString()
大多数数据收集的实现。当在 classes 中使用时,这些需要被覆盖。我知道如何对一个变量执行此操作,但如何使其对同一类型的所有变量通用?
我的 class 定义有一个变量 ToString()
覆盖:
public class Network
{
public Matrix<double> Win { get; set; } // network input matrix
public Matrix<double> Wres { get; set; } // network reservoir matrix
public Matrix<double> Wout { get; set; } // network output matrix
// constructor
public Network(Matrix<double> Winput, Matrix<double> Wreservoir, Matrix<double> Woutput)
{
Win = Winput;
Wres = Wreservoir;
Wout = Woutput;
}
public override string ToString()
{
return Win.ToString();
}
}
这可以在 Win
上使用 Console.WriteLine(network.Win.ToString());
之类的调用,但是如何输出其他矩阵 Wres
、Wout
(具有不同的维度)?我曾尝试创建三个覆盖,但由于编译器的抱怨,这不起作用:
already defines a member called 'ToString' with the same parameter types
此外,我相信一定有更通用、更优雅的方法来做到这一点。
您创建的 ToString()
方法适用于 Network
class...
换句话说,您已将调用 Network.ToString()
设为字符串化的 Win 矩阵。当然,您不能为 Network.ToString()
设置三种不同的行为 - 那么您希望它做什么?
我认为你的问题试图覆盖网络上所有矩阵字段network.matrix.ToString()
的行为class...在这种情况下,它是有必要覆盖 Matrix class 本身的行为,为此,您需要制作 Win
、Wres
和 Wout
subclass of Matrix<double>
用他们自己的 ToString()
覆盖:
public class Network
{
public NetworkMatrix Win { get; set; } // network input matrix
public NetworkMatrix Wres { get; set; } // network reservoir matrix
public NetworkMatrix Wout { get; set; } // network output matrix
// constructor
public Network(NetworkMatrix Winput, NetworkMatrix Wreservoir, NetworkMatrix Woutput)
{
Win = Winput;
Wres = Wreservoir;
Wout = Woutput;
}
...
}
public class NetworkMatrix : Matrix<double>
{
public override string ToString()
{
return "Overwridden tostring method.";
}
}
根据您提供构造函数的方式,这可能无法发送您的派生类型。另外,我还没有将 NetworkMatrix
设为通用,但如果您愿意,可以设为 NetworkMatrix<T> : Matrix<T>
。
说了这么多 - 我应该指出,调用类似 network.Win.ToString()
的东西通常是糟糕的编码实践 - 其他 class 不应该了解网络的内部结构,而你 运行 成问题。查看得墨忒耳法则(例如 https://en.wikipedia.org/wiki/Law_of_Demeter )以获取更多信息。 (特别值得一问的是 Win
、Wres
甚至 Wout
是否真的应该首先是 public?)
最好的方法是使用您对 Network.ToString()
方法的覆盖以合理的方式组合 Win、Wres 或 Wout 上的数据。或者创建一个矩阵字符串化器 class,它可以按照您想要的方式格式化字符串。
希望对您有所帮助。有什么问题,尽管问。
您已经在 Network
class 中将每个 Matrix
创建为 public 属性,因此您可以在需要时轻松访问它们到.
Network network = new Network(mInput, mResevoir, mOutput);
Console.WriteLine(network.Win);
Console.WriteLine(network.Wres);
Console.WriteLine(network.Wout);
编辑:我刚刚意识到属性也有一个 public 设置方法。如果您不希望在创建网络变量后更改属性,可以将设置器修改为私有,这样只能从 class.
中设置值public class Network
{
public Matrix<double> Win { get; private set; } // network input matrix
public Matrix<double> Wres { get; private set; } // network reservoir matrix
public Matrix<double> Wout { get; private set; } // network output matrix
// constructor
public Network(Matrix<double> Winput, Matrix<double> Wreservoir, Matrix<double> Woutput)
{
Win = Winput;
Wres = Wreservoir;
Wout = Woutput;
}
public override string ToString()
{
return Win.ToString();
}
}