We know that methods of a class can be divided into static methods and non-static methods (instance methods ). There are also many such public static methods in the. NET Framework. Now I want to discuss why a class provides static methods and when to provide static methods.
The most obvious difference between a static method and a non-static method is that if a method is public static, you can directly use the class name. the method of the method name is called, while the Public instance method needs to instantiate the object in advance before calling. Many people think that static methods are faster than the instance method in terms of memory usage, which I do not agree. Fast and slow method execution mainly depends on the operation to be performed under the same conditions, while static methods occupy more memory than instance methods. When a type is loaded, all the methods of this class will be loaded, no matter whether it is static or not, so if you think the static method takes longer memory than the instance method, this is also not true.
Why does a class provide static methods? We know that static things are related to classes, and non-static things are related to specific class instances. What is the difference between the two? The difference is that "static methods" are more common, while "instance methods" are limited. Why? Assume that there is a member class that requires the member deletion function, you may also need to provide at least two methods to delete the member corresponding to the current instance, one is to delete a specified member. The former is related to a specific instance, and the latter is not related to a specific instance. The question is here (why do we provide two methods to delete members ). You can also use this method to delete members of the current instance. But from the perspective of the object-oriented design method, do Member Zhang San have the right to delete Member Li Si? When you are happy, you can give your notebook to someone else. Can you give my notebook to someone else? Of course not, because my notebook is mine, not yours, and you have no right to do so. Member Zhang San can call his own deletion method to delete himself. When he wants to delete Member Li Si, he will not instantiate Member Li Si and ask Li Si to call his own deletion method. Otherwise, it is up to the member category to delete Member Li Si. In the real world, it is logical that Zhang San cannot delete Li Si. Therefore, a class has a static method related to the class. Do not think that static methods provide static methods with fast speed and convenient calls. This is not the case.
My example may not be very suitable, but I believe the meaning I want to express is clear. When should we provide static methods? There is such a principle (which is summarized by me), that is, if some operations do not depend on a specific instance, it is static, if some operations depend on a specific instance (for example, accessing the name of a specific member), it should be instantiated.
The above words are purely personal opinions and misleading and irresponsible.
Once a static field is assigned a value, it will wait.ProgramClose to release ......
Static classes cannot be instantiated. We directly use their attributes and methods. The biggest feature of static classes is sharing.
Exploration
Public static class statictestclass
{
Public static int n = 0;
Public static void add ()
{
N ++;
}
}
- The webpage p1.aspx calls statictestclass. Add () and outputs N on the page.
- The webpage p2.aspx calls statictestclass. Add () and outputs N on the page.
- Visitor V1 accesses p1.aspx from client C1, and the output is 1.
- Visitor V2 accesses p2.aspx from client C2, and the output is 2.
- Visitor V1 closes the browser and re-opens access p1.aspx. The output is 3.
As long as statictestclass is not re-compiled, even if p1.aspx and p2.aspx are re-compiled, Every time statictestclass. Add () is called, N will add 1 to the previous one.
Principles
- All the Members in the static class must be static.
Static Constructor
- Static classes can have static constructor, but static constructor cannot inherit.
- Static constructors can be used for static classes or non-static classes.
- The static constructor has no access modifier and no parameters. It has only one static flag.
- A static constructor cannot be called directly. A static constructor is automatically executed only once before a class instance is created or any static member is referenced.