[英]What is the best way to attach static methods to classes rather than to instances of a class?
如果我有一个计算两个整数的最大公约数的方法:
public static int GCD(int a, int b)
{
return b == 0 ? a : GCD(b, a % b);
}
将它附加到System.Math类的最佳方法是什么?
以下是我提出的三种方式:
public static int GCD(this int a, int b)
{
return b == 0 ? a : b.GCD(a % b);
}
// Lame...
var gcd = a.GCD(b);
和:
public static class RationalMath
{
public static int GCD(int a, int b)
{
return b == 0 ? a : GCD(b, a % b);
}
}
// Lame...
var gcd = RationalMath.GCD(a, b);
和:
public static int GCD(this Type math, int a, int b)
{
return b == 0 ? a : typeof(Math).GCD(b, a % b);
}
// Neat?
var gcd = typeof(Math).GCD(a, b);
所需的语法是Math.GCD
因为它是所有数学函数的标准。
有什么建议么? 我该怎么做才能获得所需的语法?
你不能。 扩展方法只是用于调用静态函数和传递特定类型实例的语法糖。 鉴于此,它们仅在实例上运行,因为它们必须通过传递要附加的类型的this
参数来定义。
我更喜欢RationalMath
那个。 你真的不需要这里的扩展方法,因为他们的目的是模仿你无法修改的对象的实例方法。 但是这里应该使用普通的旧静态方法。
鉴于您无法扩展静态Math
类,我会选择样本#2。 它遵循Math使用的模式,不会使int
方法空间混乱,并且调用简单而干净。 #3很可怕:)
就个人而言,我不会按照你想要的方式去做。 System.Math
只是一个包含一些数学函数的静态类。 。 。 没有理由它必须包含你想要使用的每个数学函数。
但是,如果你真的想要这个,我想你可以编写自己的静态Math类,它是System.Math
的一种包装器。 。 。 基本上只是通过将它传递给实际的System.Math
类来实现System.Math
的每个函数。 像这样:
public static class Math
{
public static int GCD(int a, int b)
{
return b == 0 ? a : GCD(b, a % b);
}
// Implement the System.Math methods
public static double Pow(double x, double y)
{
return System.Math.Pow(x, y);
}
// etc.
}
这似乎是一个真正的痛苦,虽然没有太大的好处。 (一种抗痉挛的糖。)但它可以让你从同一个类调用Math.GCD(a,b)
和Math.Pow(x,y)
,这听起来像你想要的。
好的,我想到的另一种方式:
namespace My
{
public static class Math
{
}
}
namespace MainApp
{
...
var gcd = My.Math.GCD(a, b);
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.