???????? Henry's VB.NET trip (4) - class and structure
??????????????????????????? Han Rui
?
Just went to work, I was found by Da Li. "Henry, what is the biggest experience of the form of the form yesterday?"
"Of course, I have experienced, the deepest impression is that the category in VB.NET is inexpensive, and the form has become a class." I said deeply.
"Yes, the class is the most basic programming structure we used to construct the VB.NET application. You have learned the most basic object-oriented programming, then you can tell me what is similar to the class. Is it different from different? "
"Okay." I replied in my mouth, my heart is still a bit unreasonable, "the structure and class are the package of members, but the class can support inheritance ..."
On the side of the big plum, he listened to me. I heard that I didn't have a voice to raise my head. "
"Nothing." I started to panic in my heart.
"Oh, I think you are in your heart, they all contain members, including constructor, methods, attributes, fields, constants, enumerations, and events. They all have shared constructor."
"Sorry, the last sentence, what is the meaning of constructor? The structure of the structure I have never defined it."
Big Li immediately wrote this code:
?
Structure Shenry
??? public x, y as integer
??? public sub new (byval x askER, BYVAL Y AS Integer)
??????? me.x = x
??????? me.y = y
??? End Sub
End structure
Sub main ()
??? DIM H1 as Shenry = new Shenry ()
??? DIM H2 As Shenry = new Shenry (2, 1)
End Sub
?
"Really, you can implement the structure of the structure!" I have always noticed this problem. "However, you only define a constructor with parameters. I understand the instantiation of H2, but how can H1 can detect by compiler?" In fact, I am more likely to ask how the structure can instantiate into objects. , That is really like it.
"Cause," Da Li pushed the push eye mirror, "each structure implicitly has a PUBLIC-free parameter constructor, which generates the default value of the structure. So you usually write constructor, or you can go out A structure, right? In fact, we cannot declare the parameter example constructor in the structural type declaration. Can only declare the 'parameterized' instance constructor. "
"You can instantiate, structures and classes are the same in memory allocation, I have been quite unknown in this question, just ask this topic.
"In this, it can be big." Seeing that Da Li smiled and smiled, I knew that I asked the idea, immediately put it, and listened carefully.
"Simply, the structure is a value type, and the class is a reference type. Therefore, the structure uses the stack allocation, and the class uses a heap assignment."
Seeing my confused eyes, big Li smiled and wrote an example on the computer:
?
Class chenry
??????? public z as integer = 0
'Initialization of non-static members is also a difference
END CLASS
Sub main ()
?? DIM H1 As Shenry = New Shenry (0, 2) 'assigns H1.x = 0 ?? DIM H2 As Shenry = H1
?? H2.x = 26
?? Dim r1 as new chenry () 'R1.z is also equal to 0
?? DIM R2 As chenry = r1
?? R2.z = 26
?? console.writeline ("h1.x =" & h1.x & ", h2.x =" & h2.x)
?? Console.WriteLine ("R1.Z =" & r1.z & ", r2.value =" & r2.z)
End Sub
?
"What should you see?" Big Li said, while running the program:
H1.x = 0, H2.x = 26
R1.z = 26, r2.Value = 26
?
Big Li looked at my glutinous eyes and said slowly: "This is the difference between value type and reference type. The structure of the instance H2.x assignment does not affect H1.x, because although they belong to a SHenry Structure, but they have their own storage space. In contrast, the R2.Z is assigned 26; the objects that are referenced by R1 and R2 are affected. "
"Something more clear, class as a reference type, is stored on the bunch of running, only by reference to this storage, can not be accessed directly. The reference type variable always contains the value reference of this type, or contains Empty reference. Empty reference does not quote anything; except for dispensing empty references, any operation that is not in-air reference is invalid. The reference type variable assignment only creates a reference to a copy, not a copy of the referenced value. They are actually pointing to the same storage area. "Big Li fingers referred to the result of the run.
"The structure is stored directly on the stack, either in an array or in another type. When the location containing the structural instance is destroyed, the structural instance will be destroyed. Value types can always be directly accessed. We can't create pairs The value of the value type is not referenced. Value type instance. Value type variable always contains the value of this type. Unlike the reference type, the value of the value type cannot be empty, and the derived is relatively large. Type object. Variable value of the value type creates a copy of the assigned value, of course, will open a memory area to save the value. "
"Oh, I understand. What else does it still?" I have a deep feeling for the difference between structural and class.
"Of course, there are many, such as all structural members, are defaulting to PUBLIC, class variables, and constant defaults to private, other class members defaults to public; structural members can not be declared as protected, class members; structural processes can not handle events, class processes Can; structural variable declarations cannot specify the initial value, the New keyword or array initial size, and the class variable declaration can. "Da Li drank water, stop, then continue.
"The structure is never terminated, so the public language runtime will never call the Finalize method in any structure; the class can be terminated by the garbage collector, the garbage collector will track unfinished references until a particular instance, when there is no remaining The garbage collector will call Finalize on the class. "
"I can understand this because the structure is a value type, is managed by the system, not reference, so it will not cause harm to memory." I then said two sentences.
"Also, you have just mentioned a very important difference between them: The structure is not inherited, and the class can be inherited. In fact, the structure itself is implicitly inherited from the ValueType class. Data type can be divided into value type and Quote Type. Value type either the stack assignment, or allocate in the inner continent mode. The reference type is allocated. The reference type and value type are derived from the final base class Object. When acting as an object, allocate a package on the heap, which enables value types to look like reference objects, and copy the value of the value type to it. The package is added to the system, so that it knows that it contains a value. Type. This process is called a pack, and its reverse process is called cancellation box. Packing and unpacking can handle any type image object. "" Oh, I understand why the structure can be instantiated into an object. "My heart is not self-ban. "Inheritance of class is often used in C and Java, but VB6 does not support inheritance, the last time you mention VB.NET to support inheritance, how did it do it!"
(Endlessly)
-------------------------------------------------- -------------
Disclaimer: The right to copyright and interpretation of this article belongs to Han Rui, if you need to reprint, please keep your full content and this statement.
QQ: 18349592
E-mail: Henry7685@hotmail.com
???? Please visit my column:
http://www.9cbs.net/develop/author/netauthor/ilatitude/