Focus
- Static Class (Sealed+abstract)
- Static constructors (no arguments, unlimited characters, automatic execution once)
- Static variables (class-level, instance-independent, static storage)
- Static methods (cannot be overridden)
- Static local variables (always present)
The static nature is an instance-independent, indicating that the instance is not modified
C + +
Static member variables
- the memory of a static member is allocated only once and will persist throughout the program's lifetime .
- If you want to initialize a static member within a class , the variable needs to be declared as a const constant .
- int class:: c = 10; Static data members can be defined and initialized outside of the class, or they can be assigned values in functions. can be accessed directly by Class::member, but cannot be used. Access
Static member functions
- member functions, static member functions, virtual functions
In C + + , the member functions of a class are stored in a static store and are accessed through a function address.
- You cannot call the Non-static function, but you can pass an indirect call by reference
- Why can't a static member function be const? The const modifier is used to indicate that a function cannot modify the value of a member variable, which must be a class member function that contains the this pointer , and the function is called ThisCall
The static function in a class is essentially a global function and cannot access non-static members, and the const function indicates that instance members are not modified, so static is meaningless.
static global variables and functionsWhen you modify a global variable, it indicates thata global variable is visible only to functions that are defined in the same file. When the function is modified, it indicates that thefunctions can only be called in the same file.
Static constConstant + visible only in the current moduleConst
in C + + is an instance member, cannot be initialized at the class definition, different objects can be different (constructs an initialization list), cannot be accessed directly by the class name
in C #The const is implicitly static and can be accessed directly by the class name. C++const is closer to read-only, soThe const of the two is different
C #
Static Class
- The nature of a static class is an abstract sealed class, so it cannot be inherited or instantiated.
- The important difference between a static class and a non-static class is that a static class cannot be instantiated, that is, a variable of a static class type cannot be created with the New keyword. Nor can there be non-static members (that is, instance members)
- Inheritance is not supported, so static classes cannot contain protected members. But interfaces can be implemented. interface cannot be static.
- A static class is usually a collection of common methods, independent of object-oriented
Execution Order (tested)Static initializers-static constructors-initializers-constructors
Static Constructors
- A member of this class executes a static constructor before being accessed for the first time . Called when the class is instantiated or when a static member is called, and the static constructor is called by the. NET Framework to initialize the static member variable .
- The static constructor is only executed once .
- Static constructors are not decorated with access restrictions because static constructors are not called by our programmers, but are called by the. NET Framework at the right time.
- The static constructor has no arguments , because the framework cannot know what parameters we need to add to the function, so the argument cannot be used.
- there can be only one static constructor in a class.
- A parameterless static constructor and a parameterless constructor can coexist. Because they belong to the class level, one belongs to the instance level and does not conflict.
- If we define a static variable in a class, but do not define a static constructor, the framework automatically generates a static constructor to initialize the class.
Static and initializer
- Initializer: The Declaration of a field is directly assigned by =
- The default initialization of the system will set everything to 0 or null before all code executes, so there is no need to use the initializer
- Static constructors run before standard constructors
Static Members
- A non-static class can contain static methods, fields, properties, or events;
- No matter how many instances are created on a class, its static members have only one copy;
- static methods and properties cannot access non-static fields and events in their containing types , and cannot access instance variables of any object;
- Variables can be defined and used in static methods, as in normal methods
- Static methods can only be overloaded, not overridden , because static methods do not belong to instance members of the class
- The nature of a constant is static: Although a field cannot be declared as static const, the behavior of a const field is inherently static. Such a field belongs to a class and does not belong to an instance of the class. Therefore, you can use classname.membername notation to access the Const field as you would with a static field;
- Can be used with static readonly (read-only differs from constant)
- The This/base keyword cannot be used in a static method because it is possible that the object does not already exist.
- When the class is loaded, all static members are created in the "static store" and once created until the program exits, it is recycled.
Note: person p; This has actually been loaded. Other classes can be accessed directly from the class name. Static variable names, just like static classes
Static Local Variables
C + +The value of a local variable in a function does not disappear after the function call, leaving the original value, that is, the storage unit it occupies is not freed, and the next time the function is called, the variable retains the value at the end of the last function call. Assigning the initial value to a static local variable is done at compile time, that is, it is assigned only one time, and it has an initial values when the program is running. The initial value is no longer re-assigned at each subsequent call to the function, but only at the end of the last function call.
C #C # does not support static variables within a method for two reasons. 1. In general, static variables within a method can replace 2 with static member variables of the class that the method belongs to. Static variables in the method can cause no more trouble in multi-threaded applications.
otherC#:
do not arrange the initialization of static member variables in the constructor of the class , because the constructor may be called repeatedly, and the initial value of the variable should only be set once. C + +: Also do not put initialization action in the header file, because it may be included in many places, so it may be executed many times. YouThe initial value should be set in the implementation file and anywhere outside the category. For example, in main, or in a global function, or outside of any function
"Summarize" static modifiers in C + + and C #