???????????? henry's VB.NET trip (5) - inheritance
??????????????????????????????? Han Rui
?
"The inheritance of the class?" Big Li Nodded, "In the past, many people criticized VB's functionality, they would add this condition. But now VB.NET is already a relatively perfect object-oriented programming method. Just now we also mention It is similar to that, the class is similar to the structure of VB.NET, allowing us to define the data type of the package. However, an important difference from the structure is that the VB.NET class can inherit and extend other classes. As new class The base class is called 'base class'. The class derived from the base class is called 'derived class'. All fields, attributes, methods, methods, methods, methods defined in the base class, are derived, attribute, methods, and events. You see, we only need to make a class Developing and debugging, they can be reused as other classes. "
"The concept of inheritance of the class I have learned, how do you come in VB.NET?" I still insisted.
"I write to you, see it, as the saying goes, is it better to see?" Da Li ridiculed two sentences, and started to knock on the keyboard:
Public Class CBasehenry
???PriVate x as integer = 10
?? Protected y as integer
?? Sub new ()
?????? console.writeLine ("Structure of the base class")
??? End Sub
??? protected overrides sub firmize ()
?????? console.writeline ("Destructure of the Base")
???? mybase.finalize ()
??? End Sub
Public Overridable Function GetY (Byval X As Integer) AS INTEGER
??? me.y = me.x x? 'Private type ME.X can only be used in the base class
??????? console.writeline ("The result of the base class, the result is:" & me.y)
??????? Return ME.Y
??? End function
??? public sub Oldy ()
??????? console.writeLine ("Oldy method of the base class")
End Sub
END CLASS
?
"You see, this is a base class, I will write you a derived class inherited from the class." He said while continuing to write:
Public Class CDeriveDhenry
?? inherits CBasehenry
?? Sub new ()
?? ???? mybase.new () 'Note: This sentence is to be placed in the first sentence in SUB
????? console.writeline ("Configuration of Delivery Class")
??? End Sub
??? protected overrides sub firmize ()
?????? console.writeline ("Destructure of Delivery Class")
?????? mybase.finalize ()
??? End Sub
??? public overrides function game (byval x as integer) AS INTEGER
?????? me.y = x * 10 'protected type ME.Y can be used in derived class
?????? console.writeline ("Gety method of derived class, the result is:" & me.y)
?????? Return ME.Y
??? End function
??? public sub newy () ??????? console.writeline ("New Method for Delivery Class")
??? End Sub
END CLASS
?
?
"You see it, we identify who the base class of this class is, if we don't identify the class, such as CBasehenry, VB.NET, will be derived from Object base class, this is a species Implicit inheritance, and structures are implicitly inherited from the ValueType class. "Big Li pointed to me with me.
"What does these Overrides and OVERLOAD identities do?" I felt their meaning, but I couldn't clearly say it.
"We use inheritance, but also allow some attributes or methods in derived classes to have different behaviors of the base class, we want 'to rewrite' it, but the base class has to agree to you can override, otherwise it will not Did you have a naming conflict? Because the object of the derived class is the public member of the base class, how do you know which base classes have rewritten by the derived class? So you must have an agreement: We are in the base In the class, use the Overridable modifier to identify the properties or methods in the base class or method in derived class, no identifiable, is actually the default is an NOTOVERRIDABLE modifier to implicitly identify, to remind the compiler that the property or The method cannot be rewritten; and then when derived, we use the Overrides modifier to identify which is overridable attributes or methods defined in the base class. "Da Li said," Let's take a meal Class operation. "
Public Sub Main ()
??? DIM obj as cderivedhenry = new cderivedhenry ()
??? Obj.gety (4) ??? 'Calling the getY method of the derived class
??? Obj.oldy () ???? 'Call the Oldy method of the base class
??? obj.newy () ???? 'Newy method to call the derived class
End Sub
?
Then Da Li pressed the F5 key and appeared as follows in the "Output" window:
The construction of the base class ??????????????????? 'started to run new cderivedhenry ()
Detective structure
The get method of the derived class, the result: 40 'obj.gety (4) operation results = 4 * 10, inexpensive (4 10)
OLDY method of the base class? 'Obj.oldy () operation results
New method for derived class? 'Obj.newy () operation results
Concentrated sect
Destructure of the base class
?
"You see, you only instantly customize a derived object in main, why will the 'base class constructive'?" Da Li asked.
"This," I started to view it in the code, "it was this sentence." I finger the code in the derived class:
Sub new ()
?? ???? mybase.new () 'Note: This sentence is to be placed in the first sentence in SUB
?????? console.writeline ("Configuration of Delivery")
End Sub
?
"Yes, we must pay attention to this, that is, the derived constructor must be rewritten. NEW constructor is generally used to open files, connect to database, initialization variables, and processing any need to be used Completed other tasks. We must use the statement mybase.new () in the first line code in the Sub New constructor to call the constructor of the base class in the class hierarchy to obtain the nature of the base class. Destructure The cleaning task for performing the pair of faults in Sub Finalize, such as saving status information, closing files, and connection with the database, and performing the last sentence of the designer function after the other tasks must be completed before the object is released Statement mybase.finalize () Explicitly calls its base class's Sub Finalize method to destructure mybase.new () constructor. So you can see this order from program operation results. "Da Li Tap the screen, like a wake up for me. "I understand, I will remember." I honestly responded.
Dafu suddenly remembered what, looked up and said to me: "Telling the inheritance of the class, we have to look at the overload and hidden problems."
?
(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/