Constructor and destructor

xiaoxiao2021-03-06  57

Using system; public class myclass1 {public class myclass1 {public myclass1 () // If there is a next constructor, and this class is inherited, this type of non-parametric cannot be less. {Console.WriteLine ("Myclass1 parameterless contructor!");} public myclass1 (String param1) {console.writeline ("Myclass1 Constructor Parameters:" param1);}}

Public class myclass2: myclass1 {public myclass2 () {Console.writeline ("Myclass2 parameterless contructor!");} ~ myclass2 () {console.writeline ("MyClass2 des");}

}

Public class test {public static void main () {myclass2 myObject1 = new myclass2 ();}} ============================= ==== using System; public class MyClass1 {public MyClass1 () {Console.WriteLine ( "! MyClass1 Parameterless contructor");} public MyClass1 (string param1) {Console.WriteLine ( "MyClass1 Constructor Parameters:" param1); }

Public class myclass2: myclass1 {public myclass2 (string param1): base (param1) / / Specifies the constructor of the tape parameters of the base class, and the other can save (if not specified, if not specified, call no parameters) At this time, it is not possible to save) {Console.Writeline ("Myclass2 Construction Prameters:" param1);}} public class test {miClass2 myobject1 = new myclass2 ("hello");}}}}} The last executed is the statement in this constructor before the parent class constructor is called. In other words, the priority of the variable initialization is the highest.

We see a public modifier in the statement of the class constructor, and of course there can be protected / private / internal modifiers. According to the modifier rules, if we modify a class constructor as private, then when we inherit the class, we will not call this Private constructor. Will we inherit it? It is this. In fact, such a class in our class is still static (STIC), but does not want the user to instantiate it, and the compiler must be shielded to our secretizer (compiler) The added constructor is public), it is necessary to make a private instance constructor. Protected / Internal also has similar usage.

Static variable in the static constructor initialization. The static constructor is not invisible in the inheritance as an instance constructor, and cannot be called directly by the user. Mastering the static constructor is to master its execution time. The implementation of the static constructor is not determined (the compiler is not clearly defined). However, there are four guidelines to be mastered: 1. During the implementation of a program, the static constructor is only once. 2. The static constructor is executed after initialization of static members of the class. Or tell the compiler to convert a static member initialization statement into an assignment statement at the beginning of the static constructor. 3. The static constructor is executed before any class of static members. 4. The static constructor is executed before any class of instance variables.

It is worth noted that the instance constructor can reference the instance variable, or the static variable can also be referenced. The static variable can be referenced in the static constructor. This is easy to understand in the semantics of the class and objects.

In fact, if we can deeply grasp the unique purpose of the structure of the class is to ensure that the members in the class can get the correct initialization, we have a good understanding of the various C # medium-shaped structures - it has no reason such!

Destructor

The sectors are only responsible for recycling those non-system resources, relatively typical as: open files, obtained window handles, database connections, network connections, etc. need users to release non-memory resources.

转载请注明原文地址:https://www.9cbs.com/read-117153.html

New Post(0)