简体   繁体   English

将静态方法附加到类而不是类的实例的最佳方法是什么?

[英]What is the best way to attach static methods to classes rather than to instances of a class?

If I have a method for calculating the greatest common divisor of two integers as: 如果我有一个计算两个整数的最大公约数的方法:

public static int GCD(int a, int b)
{
    return b == 0 ? a : GCD(b, a % b);
}

What would be the best way to attach that to the System.Math class? 将它附加到System.Math类的最佳方法是什么?

Here are the three ways I have come up with: 以下是我提出的三种方式:

public static int GCD(this int a, int b)
{
    return b == 0 ? a : b.GCD(a % b);
}

// Lame...

var gcd = a.GCD(b);

and: 和:

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

and: 和:

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

The desired syntax is Math.GCD since that is the standard for all mathematical functions. 所需的语法是Math.GCD因为它是所有数学函数的标准。

Any suggestions? 有什么建议么? What should I do to get the desired syntax? 我该怎么做才能获得所需的语法?

You cannot. 你不能。 Extension methods are just syntactic sugar for calling a static function and passing an instance of a particular type. 扩展方法只是用于调用静态函数和传递特定类型实例的语法糖。 Given that, they operate only on instances, as they must be defined by passing a this parameter of the type you want to attach to. 鉴于此,它们仅在实例上运行,因为它们必须通过传递要附加的类型的this参数来定义。

I would prefer the one with RationalMath . 我更喜欢RationalMath那个。 You really don't need extension methods here, because their aim is to mimic instance methods of objects of you can't modify. 你真的不需要这里的扩展方法,因为他们的目的是模仿你无法修改的对象的实例方法。 But here one should use plain old static method. 但是这里应该使用普通的旧静态方法。

Given the fact that you cannot extend the static Math class I would go for sample #2. 鉴于您无法扩展静态Math类,我会选择样本#2。 It follows the pattern used by Math, does not clutter the int method space, and is simple and clean to invoke. 它遵循Math使用的模式,不会使int方法空间混乱,并且调用简单而干净。 #3 is plain horrible :) #3很可怕:)

Personally, I wouldn't do it the way you want. 就个人而言,我不会按照你想要的方式去做。 System.Math is just one static class that contains some mathematical functions . System.Math只是一个包含一些数学函数的静态类。 . . there's no reason it has to contain every mathematical function you'd ever want to use. 没有理由它必须包含你想要使用的每个数学函数。

However, if you really want this, I suppose you could write your own static Math class that's a sort of wrapper for System.Math . 但是,如果你真的想要这个,我想你可以编写自己的静态Math类,它是System.Math的一种包装器。 . . basically just implement every function in System.Math by passing it along to the actual System.Math class. 基本上只是通过将它传递给实际的System.Math类来实现System.Math的每个函数。 Like this: 像这样:

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.
}

This seems like a real pain in the neck though for not much benefit. 这似乎是一个真正的痛苦,虽然没有太大的好处。 (Kind of an anti -syntactic sugar.) But it would let you call Math.GCD(a,b) and, say, Math.Pow(x,y) from the same class, which is what it sounds like you want. (一种痉挛的糖。)但它可以让你从同一个类调用Math.GCD(a,b)Math.Pow(x,y) ,这听起来像你想要的。

Ok, one other way I thought of: 好的,我想到的另一种方式:

namespace My
{
    public static class Math
    {
    }
}


namespace MainApp
{
    ...
    var gcd = My.Math.GCD(a, b);
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用常用方法构造 static 常量类的最佳方法 - Best way to structure static constant classes with common methods 平稳移动游戏对象而不是传送对象的最佳方法是什么? - What is the best way to move game objects, smoothly rather than teleporting it? 类,使用方法的最佳方法是什么? - Class, What's the best way to use methods? 将 xml 数据发送到 web 服务而不是使用 CDATA 的最佳方法是什么? - What is the best way to send xml data to a web service rather than using CDATA? 将标签附加到枚举值的最佳方法是什么 - What is the best way to attach a label to an enum value 嵌套 static 个没有方法的类有什么好处? - What is the benefit of nesting static classes without methods? 使用子类和方法的最佳方法 - The best way to use child classes and methods 模拟静态类继承的最佳方式 - Best way to emulate inheritance of static classes 为什么C#将匿名方法和闭包实现为实例方法,而不是静态方法? - Why does C# implement anonymous methods and closures as instance methods, rather than as static methods? 向第三方开发人员公开我的核心API的关键类/方法的最佳方法是什么? - What is the best way expose key classes/methods my core API to 3rd party developers?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM