简体   繁体   English

Class 方法与实例方法

[英]Class methods vs instance methods

Hi Are class methods generally measured to be faster than instance methods since it doesn't require loading an instance?嗨,class 方法是否通常比实例方法更快,因为它不需要加载实例? If so, should we use class methods when possible?如果是这样,我们是否应该尽可能使用 class 方法? Thanks谢谢

Regardless of what is faster and how much, there is one major difference that you need to remember:不管什么更快,多少,您需要记住一个主要区别:

  • You cannot @Override a static method!您不能@Override一个 static 方法!

This is very important because you essentially say that you will not, and cannot , use one of the major advantages in Java, namely overriding methods in sub-classed objects.这非常重要,因为您本质上说您不会也不能使用 Java 中的主要优势之一,即覆盖子类对象中的方法。 When you call a static method, you stay with that static method and cannot override it in sub-classed objects.当您调用 static 方法时,您将继续使用该 static 方法,并且不能在子类对象中覆盖它。

Also to resolve the "which is faster" then construct a REAL test, not just a microbenchmark to investigate the actual findings.还要解决“哪个更快”,然后构建一个真正的测试,而不仅仅是一个微基准来调查实际发现。 Use several JVM's to measure because JIT implementation may influence this.使用多个 JVM 进行测量,因为 JIT 实现可能会影响这一点。

If a method doesn't require an instance, IMO it should be a class method.如果一个方法不需要实例,IMO 它应该是一个 class 方法。 And since a class method is only possible if you don't use the instance, then your question而且由于 class 方法只有在您不使用实例的情况下才有可能,那么您的问题

should we use class methods when possible我们是否应该尽可能使用 class 方法

has a positive answer.有一个肯定的答案。

But definitely NOT for efficiency reasons但绝对不是出于效率原因

No, they are NOT faster.不,它们并不快。

However, it's a good practice to use class methods whenever that's possible, because thus you are indicating that the logic inside the method doesn't require to access any member variables.但是,只要有可能,最好使用 class 方法,因为这样您就表明方法内部的逻辑不需要访问任何成员变量。

I am saying - don't use instance methods, which can be static.我是说 - 不要使用实例方法,它可以是 static。

A "class method" is available for every instance of the class and the "instance methods" juste for the current instance. class 的每个实例和当前实例的“实例方法”都可以使用“类方法”。 So i don't see why a class method will be faster when it applies to all intances...所以我不明白为什么 class 方法在应用于所有实例时会更快......

While class methods may be faster, you should definitely not write code with that way of thinking.虽然 class 方法可能更快,但您绝对不应该以这种思维方式编写代码。 You should use a class method when you need them.您应该在需要时使用 class 方法。 Utility classes like Arrays are a good example. Arrays 等实用程序类就是一个很好的例子。 Factories that return a singleton.返回 singleton 的工厂。 Never use them when you require access to the internals of a class.当您需要访问 class 的内部时,切勿使用它们。

When comparing class methods and instance methods, try to think of instance methods as class methods that have a extra parameter called this (In fact that is now some languages implement instance methods)在比较 class 方法和实例方法时,尽量把实例方法想象成 class 方法有一个额外的参数叫做this (其实现在有些语言实现了实例方法)

So the question becomes, "will my method be faster if it has one less parameter?"所以问题变成了,“如果我的方法少一个参数,它会更快吗?” and that question does not really make sense, because the parameter list is largely irrelevant to the performance.这个问题并没有真正的意义,因为参数列表在很大程度上与性能无关。

Try to base the decision of whether a method should be static or instance on the nature of the method, and on the data it requires, not on some premature performance benefit.尝试根据方法的性质和所需的数据来决定方法是否应该是 static 或实例,而不是基于一些过早的性能优势。 Yes, performance is a feature, but it's not the only feature.是的,性能是一个特性,但它不是唯一的特性。

One last performance rule of thumb: Measure, measure, measure.最后一条性能经验法则:测量、测量、测量。 Just because some book or article said that something should be faster, doesn't mean that it will work for you.仅仅因为一些书或文章说某事应该更快,并不意味着它会为你工作。 Try it on your real-world case and back it up with empirical data.在您的真实案例中进行尝试,并使用经验数据进行备份。

From my experience, if you need to initialise an object and retain it in any sort of manner, eg using an array etc. Its best to call an instance method on that particular instance.根据我的经验,如果您需要初始化 object 并以任何方式保留它,例如使用数组等。最好在该特定实例上调用实例方法。
Theres no point calling a Class Method then passing that same instance you just initialised as an argument to that Class Method.调用 Class 方法然后将您刚刚初始化的同一个实例作为参数传递给该 Class 方法是没有意义的。 I'm unsure on the effect during runtime, but doing this seems to be a waste (nominal or not).我不确定运行时的效果,但这样做似乎是一种浪费(名义上的与否)。

I mainly use class methods for operations that dont need to be initialised, whenever I can.我主要使用 class 方法进行不需要初始化的操作,只要我可以。 For example, my MathFunctions class contains all my getters to my trigonometric methods.例如,我的 MathFunctions class 包含了我所有的三角函数方法。 Theres no point initialising and creating a MathFunctions object to then call an instance method simply to get an arbitrary result from one of these methods.没有必要初始化和创建一个 MathFunctions object 然后调用实例方法只是为了从这些方法之一获得任意结果。 Its easier (and faster) to simply call the class method.简单地调用 class 方法更容易(也更快)。

So in either case, there is no "Class Method > Instance Method" or visa-versa.所以在任何一种情况下,都没有“类方法>实例方法”或反之亦然。 It simply depends on your application and what your needing.这仅取决于您的应用程序和您的需求。 Use common sense above all, if you find yourself initialising objects for classes that hold minimal data (Eg MathFunctions) your probably better off with a Class Method.首先使用常识,如果您发现自己为保存最少数据的类(例如 MathFunctions)初始化对象,那么使用 Class 方法可能会更好。
But on the flipside, if you find yourself initialising objects then passing them into Class Methods as arguments, your most likely going to be better off with an Instance Method.但另一方面,如果您发现自己正在初始化对象,然后将它们作为 arguments 传递给 Class 方法,那么使用实例方法很可能会更好。

Thats my two-cents, I'm still relatively new to programming, so keep that in mind.那是我的两分钱,我对编程还是比较陌生,所以请记住这一点。

I don't know about generally, but I remember measuring for some application some time ago, and static methods were indeed faster.我一般不知道,但我记得前段时间测量了一些应用程序,static 方法确实更快。

From a design standpoint I would argue that any method than can sensibly be static (meaning without explicitly passing an instance as parameter or something like that), should be.从设计的角度来看,我认为任何可以合理地使用 static 的方法(意味着没有明确地将实例作为参数或类似的东西传递)都应该是。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM