方法何时应该是静态的?

时间:2022-09-04 10:55:07

In addition, are there any performance advantages to static methods over instance methods?

另外,静态方法比实例方法有任何性能优势吗?

I came across the following recently: http://www.cafeaulait.org/course/week4/22.html :

我最近遇到了以下内容:http://www.cafeaulait.org/course/week4/22.html:

When should a method be static?

方法何时应该是静态的?

  1. Neither reads from nor writes to instance fields
  2. 既不读取也不写入实例字段

  3. Independent of the state of the object
  4. 独立于对象的状态

  5. Mathematical methods that accept arguments, apply an algorithm to those arguments, and return a value
  6. 接受参数的数学方法,将算法应用于这些参数,并返回值

  7. Factory methods that serve in lieu of constructors
  8. 用于代替构造函数的工厂方法

I would be very interested in the feedback of the Stack Overflow community on this.

我对Stack Overflow社区的反馈非常感兴趣。

8 个解决方案

#1


21  

Make methods static when they are not part of the instance. Don't sweat the micro-optimisations.

当方法不是实例的一部分时,使方法保持静态。不要为微观优化而烦恼。

You might find you have lots of private methods that could be static but you always call from instance methods (or each other). In that case it doesn't really matter that much. However, if you want to actually be able to test your code, and perhaps use it from elsewhere, you might want to consider making those static methods in a different, non-instantiable class.

您可能会发现有许多私有方法可能是静态的,但您总是从实例方法(或彼此)调用。在那种情况下,这并不重要。但是,如果您希望实际能够测试代码,并且可能从其他地方使用它,您可能需要考虑在不同的,不可实例化的类中创建这些静态方法。

#2


18  

Whether or not a method is static is more of a design consideration than one of efficiency. A static method belongs to a class, where a non-static method belongs to an object. If you had a Math class, you might have a few static methods to deal with addition and subtraction because these are concepts associated with Math. However, if you had a Car class, you might have a few non-static methods to change gears and steer, because those are associated with a specific car, and not the concept of cars in general.

方法是否是静态的更多是设计考虑而不是效率。静态方法属于一个类,其中非静态方法属于一个对象。如果您有Math类,则可能有一些静态方法来处理加法和减法,因为这些是与Math相关的概念。但是,如果你有一个Car类,你可能会有一些非静态的方法来改变齿轮和转向,因为它们与特定的汽车相关联,而不是一般的汽车概念。

#3


12  

Another problem with static methods is that it is quite painful to write unit tests for them - in Java, at least. You cannot mock a static method in any way. There is a post on google testing blog about this issue.

静态方法的另一个问题是,为Java编写单元测试是非常痛苦的 - 至少在Java中。你不能以任何方式模拟静态方法。谷歌测试博客上有一篇关于此问题的帖子。

My rule of thumb is to write static methods only when they have no external dependencies (like database access, read files, emails and so on) to keep them as simple as possible.

我的经验法则是只有在没有外部依赖关系(如数据库访问,读取文件,电子邮件等)时才编写静态方法,以使它们尽可能简单。

#4


2  

@jagmal I think you've got some wires crossed somewhere - all the examples you list are clearly not static methods.

@jagmal我认为你已经在某处交叉了一些电线 - 你列出的所有例子显然都不是静态方法。

Static methods should deal entirely with abstract properties and concepts of a class - they should in no way relate to instance specific attributes (and most compilers will yell if they do).

静态方法应该完全处理类的抽象属性和概念 - 它们绝不应该与实例特定的属性相关(如果大多数编译器都会大喊大叫)。

For the car example, speed, kms driven are clearly attribute related. Gear shifting and speed calculation, when considered at the car level, are attribute dependent - but consider a carModel class that inherits from car: at this point theyy could become static methods, as the required attributes (such as wheel diameter) could be defined as constants at that level.

对于汽车的例子,速度,kms驱动显然属性相关。在汽车级别考虑时,换档和速度计算取决于属性 - 但考虑从汽车继承的carModel类:此时它们可能成为静态方法,因为所需的属性(例如车轮直径)可以定义为那个级别的常量。

#5


2  

Just remember that whenever you are writing a static method, you are writing an inflexible method that cannot have it's behavior modified very easily.

请记住,无论何时编写静态方法,您都在编写一种不灵活的方法,不能轻易地修改它的行为。

You are writing procedural code, so if it makes sense to be procedural, then do it. If not, it should probably be an instance method.

你正在编写程序代码,所以如果程序化是有道理的,那就去做吧。如果没有,它应该是一个实例方法。

This idea is taken from an article by Steve Yegge, which I think is an interesting and useful read.

这个想法取自Steve Yegge的一篇文章,我认为这是一篇有趣且有用的读物​​。

#6


1  

Performance-wise, a C++ static method can be slightly faster than a non-virtual instance method, as there's no need for a 'this' pointer to get passed to the method. In turn, both will be faster than virtual methods as there's no VMT lookup needed.

在性能方面,C ++静态方法可能比非虚拟实例方法稍快,因为不需要将“this”指针传递给方法。反过来,两者都比虚拟方法更快,因为不需要VMT查找。

But, it's likely to be right down in the noise - particularly for languages which allow unnecessary parameter passing to be optimized out.

但是,它可能会在噪音中正确 - 特别是对于允许不必要的参数传递进行优化的语言。

#7


0  

Here is a related discussion as to why String.Format is static that will highlight some reasons.

这是一个相关的讨论,为什么String.Format是静态的,将突出一些原因。

#8


0  

Another thing to consider when making methods static is that anyone able to see the class is able to call a static method. Whereas when the mehtod is an instance method, only those who have access to an instance are able to call that method.

在使方法静态时要考虑的另一件事是,任何能够看到该类的人都能够调用静态方法。虽然mehtod是实例方法,但只有那些有权访问实例的人才能调用该方法。

#1


21  

Make methods static when they are not part of the instance. Don't sweat the micro-optimisations.

当方法不是实例的一部分时,使方法保持静态。不要为微观优化而烦恼。

You might find you have lots of private methods that could be static but you always call from instance methods (or each other). In that case it doesn't really matter that much. However, if you want to actually be able to test your code, and perhaps use it from elsewhere, you might want to consider making those static methods in a different, non-instantiable class.

您可能会发现有许多私有方法可能是静态的,但您总是从实例方法(或彼此)调用。在那种情况下,这并不重要。但是,如果您希望实际能够测试代码,并且可能从其他地方使用它,您可能需要考虑在不同的,不可实例化的类中创建这些静态方法。

#2


18  

Whether or not a method is static is more of a design consideration than one of efficiency. A static method belongs to a class, where a non-static method belongs to an object. If you had a Math class, you might have a few static methods to deal with addition and subtraction because these are concepts associated with Math. However, if you had a Car class, you might have a few non-static methods to change gears and steer, because those are associated with a specific car, and not the concept of cars in general.

方法是否是静态的更多是设计考虑而不是效率。静态方法属于一个类,其中非静态方法属于一个对象。如果您有Math类,则可能有一些静态方法来处理加法和减法,因为这些是与Math相关的概念。但是,如果你有一个Car类,你可能会有一些非静态的方法来改变齿轮和转向,因为它们与特定的汽车相关联,而不是一般的汽车概念。

#3


12  

Another problem with static methods is that it is quite painful to write unit tests for them - in Java, at least. You cannot mock a static method in any way. There is a post on google testing blog about this issue.

静态方法的另一个问题是,为Java编写单元测试是非常痛苦的 - 至少在Java中。你不能以任何方式模拟静态方法。谷歌测试博客上有一篇关于此问题的帖子。

My rule of thumb is to write static methods only when they have no external dependencies (like database access, read files, emails and so on) to keep them as simple as possible.

我的经验法则是只有在没有外部依赖关系(如数据库访问,读取文件,电子邮件等)时才编写静态方法,以使它们尽可能简单。

#4


2  

@jagmal I think you've got some wires crossed somewhere - all the examples you list are clearly not static methods.

@jagmal我认为你已经在某处交叉了一些电线 - 你列出的所有例子显然都不是静态方法。

Static methods should deal entirely with abstract properties and concepts of a class - they should in no way relate to instance specific attributes (and most compilers will yell if they do).

静态方法应该完全处理类的抽象属性和概念 - 它们绝不应该与实例特定的属性相关(如果大多数编译器都会大喊大叫)。

For the car example, speed, kms driven are clearly attribute related. Gear shifting and speed calculation, when considered at the car level, are attribute dependent - but consider a carModel class that inherits from car: at this point theyy could become static methods, as the required attributes (such as wheel diameter) could be defined as constants at that level.

对于汽车的例子,速度,kms驱动显然属性相关。在汽车级别考虑时,换档和速度计算取决于属性 - 但考虑从汽车继承的carModel类:此时它们可能成为静态方法,因为所需的属性(例如车轮直径)可以定义为那个级别的常量。

#5


2  

Just remember that whenever you are writing a static method, you are writing an inflexible method that cannot have it's behavior modified very easily.

请记住,无论何时编写静态方法,您都在编写一种不灵活的方法,不能轻易地修改它的行为。

You are writing procedural code, so if it makes sense to be procedural, then do it. If not, it should probably be an instance method.

你正在编写程序代码,所以如果程序化是有道理的,那就去做吧。如果没有,它应该是一个实例方法。

This idea is taken from an article by Steve Yegge, which I think is an interesting and useful read.

这个想法取自Steve Yegge的一篇文章,我认为这是一篇有趣且有用的读物​​。

#6


1  

Performance-wise, a C++ static method can be slightly faster than a non-virtual instance method, as there's no need for a 'this' pointer to get passed to the method. In turn, both will be faster than virtual methods as there's no VMT lookup needed.

在性能方面,C ++静态方法可能比非虚拟实例方法稍快,因为不需要将“this”指针传递给方法。反过来,两者都比虚拟方法更快,因为不需要VMT查找。

But, it's likely to be right down in the noise - particularly for languages which allow unnecessary parameter passing to be optimized out.

但是,它可能会在噪音中正确 - 特别是对于允许不必要的参数传递进行优化的语言。

#7


0  

Here is a related discussion as to why String.Format is static that will highlight some reasons.

这是一个相关的讨论,为什么String.Format是静态的,将突出一些原因。

#8


0  

Another thing to consider when making methods static is that anyone able to see the class is able to call a static method. Whereas when the mehtod is an instance method, only those who have access to an instance are able to call that method.

在使方法静态时要考虑的另一件事是,任何能够看到该类的人都能够调用静态方法。虽然mehtod是实例方法,但只有那些有权访问实例的人才能调用该方法。