Static keyword Everyone is sure to use a lot of, said everyone also know, what is there to talk about?
But back to some of the previous things, inevitably call my heart ...
The cause is that some of the classes in a project are all methods, but are designed as non-static classes. So I was trying to persuade my colleagues to design these classes as static and to make a great effort. I was exhausted when my colleagues understood it.
Don't know if you have the following ideas?
- Static is from the process-oriented to object-oriented legacy, the past development is process-oriented, so many use static classes, static methods, to object-oriented, the keyword is preserved. Therefore, it is best not to use the static keyword in object-oriented development.
- Whether it's a static class or a non-static class, the effect is the same, no difference.
- If you have to let me use static classes, what is the point of doing this?
- If I put the non-static class as a static class, then what's the problem?
Below for some of the above views, the author elaborated his understanding, if there is no place to write the wrong, please also point out.
- The static keyword is indeed a keyword for the era of process development, but it does not mean that the static keyword should be avoided as much as possible in object-oriented development.
- Static class and non-static class effect is certainly not the same, there must be some difference.
- Based on the author's experience, the static class is generally used for classes that are not state-independent. So, what is a state-independent class? My understanding is that when there are no attributes in a class, only the method can be considered a class that is not state-independent. Recall that a non-static class instantiates an object, where is the purpose? The purpose is to save the state of the class through this instantiated object.
Isn't it? For example
A = new A ();
A.name = "Kevin";
A.sex = "male";
......
The state of the class is persisted by object A through the object a that we instantiate.
If our class has no attributes and is all methods, then is it necessary to instantiate an object in order to invoke the method in the class?
Therefore, I suggest that, in the design of the class, if the class is a state-independent class, it is recommended that it be designed as a static class.
As for the meaning of this: first, the performance loss of constructing an object is omitted, although it can be neglected. Second, the programmer is more friendly, the method of invoking the class is more convenient.
- As long as a class is a class independent of the State class, it is no problem to design it as a static class.
For some of these views, it is purely personal experience. If you have a different point of view, welcome to communicate together.
Static in C #