C # sharp experience the thirteenth lecture

xiaoxiao2021-03-06  69

Thirteenth lecture

The structure class provides the first other support for us to build a reuse component in an object-oriented manner. However, sometimes we want to get a lightweight data type that can be allocated cheap and quickly like a basic type of system built in the system, and does not inherit the lightweight data type of the burden. Customized struct types in C # provide us with this implementation, especially for building some smaller data structures. ??????? Structure and classes are very similar, such as they can include members such as domain, methods, attributes, events, indexes, and structures can also implement multiple interfaces. However, there are many differences in structures and classes, the most typical structure, and the class is the reference type. The value type makes the structural variable itself contains structural data, which are allocated on the stack, not the reference type of the class, etc., is assigned on the hosted stack. Value class semantics is the core nature of the structure, which is derived from its various behaviors. The assignment of the structure is to pay attention to, see the following code: use system; structure {???? public int x, y; ???? public spoint (int x, int y)? ??? {???????? this.x = x; ???????? this.y = Y; ????}} class cpoint // point class {???? public Int x, y; ???? public cpoint (int X, int y) ???? {???????? this.x = x; ???????? THIS.Y = Y ; ????}} class test {???? static void main () ???? {???????? spoint spa = new spoint (10, 20); // point structure??? ????? spoint spb = spa; // point structure assignment ???????? SPB.x = 0; ???????? console.writeLine (spa.x "," SPA. Y); // 10, 20 ???????? console.writeline (spb.x "," spb.y); // 0, 20 ???????? cpoint CPA = New CPOINT (10, 20); // Category ???????? cpoint CPB = CPA; // Category assignment ???????? cpb.x = 0; ???????? Console.writeline (CPA.X "," CPA.Y); // 0, 20 ???????? console.writeline (CPB.x "," CPB.Y); // 0, 20 ????}} ??????? The assignment of the structure causes a copy of the data within the structure, not the copy of the handle! The same most parameter or return value, the conveyed structure is also a new copy of its data member. ??????? Structure instance constructor and this Thiis in the instance member function is also different from the meaning in the class. In the class, this is essentially a reference handle value, which cannot be assigned again. In the structure, THIS is more like a variable of a structural type, which can be assigned. ??????? The default value of the structural type is to set all the data members of all the value types to the default value of the corresponding value, and set the data member of the reference type to NULL. The default value of the class is NULL. Since the structural variable itself contains its data, it is not possible to assign a structural variable to a null - the structure does not quote the concept of the handle, and there is no NULL value! The default value rules for value types make the structure that no parameters are declared. For instance members, the structure is not allowed to initialize while the declaration. However, there is no such provision for static members, or a static constructor that implements non-parameters can also be declared. The declaration is not allowed to implement the destructor, which is determined by the nature of its stack assignment.

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

New Post(0)