Static methods and static properties

Not all of the dynamic

we all know,The method is put in the running instance。That is the result of the implementation of the method is based on the instance property came,The property is an example of the current state of change - we call it "dynamic"。

But really all it is dynamic? In fact, not,Some methods give results which will directly and according to your call,It does not need to change according to the change of the instance attribute-that is to say, it will produce a definite result anyway,We call such methods static methods,It is static。

Let's take a look:

This is an ordinary class and method,We create different Gun instances,Then fire() The execution result of the method will also be different,Some bullets are finished,Some are not finished。

Now I have inserted a method to determine the size of two numbers in this class,This method does not matter what state the instance is in,It will always return a definite result-whichever number is greater,It has nothing to do with the status of the instance-it doesn't matter how many shots are shot,Let’s say that this method can be set as a static method (marked with the static prefix)。As you can see,Static method calls do not require an instance,Directly use the dot operator to call。

"A behavior that does not rely on instance variables and does not require objects。”

Type method

We use the prefix static to mark static methods,Since we call the static method and not call it from a specific instance,But call it directly from the class,We also say that this method is implemented at the "class" level,So static methods will also be called "Type method”。

Same reason,It will also cause the type method not to target a specific instance,So it can't access instance properties-type methods can't access dynamic properties。It's not on the heap at all,Naturally, a certain attribute in a certain instance on the heap is not found!

So again for the same reason,Type methods cannot call dynamic methods-dynamic methods are on the heap,In the example!

Type attribute

We said that type methods cannot access dynamic properties,Then there will naturally be static properties! of course,Dynamic methods can also access static properties-considering that static is at the class level,So it will also be called "Type attribute”。

Same reason,Share with all objects of the same kind!

That's right! It depends on it for objects of the same kind to communicate with each other! Let's take a look:

We have added a monitor to monitor the number of bullets fired by all guns,Fired five shots in three instances,But total is recorded-that is to say, the type attribute is global for the same kind of instance,It does not belong to an object,It belongs to the objects encountered in this category!

Instance attributes-one for each instance;

Type attributes-one for each class!

Static and inheritance

If you are careful,You may find that type attributes and type methods cannot be overridden! If you override a type method,Will be prompted:You are overriding a method marked not final!

so,Can be overwritten if needed,You need to use class prefix instead of static to mark type methods,So it allows to be overridden by subclasses。

Original article written by LogStudio:R0uter's Blog » Static methods and static properties

Reproduced Please keep the source and description link:https://www.logcg.com/archives/1136.html

About the Author

R0uter

The non-declaration,I have written articles are original,Reproduced, please indicate the link on this page and my name。

Leave a Reply

Your email address will not be published. Required fields are marked *