What is inheritance? Why should why use inheritance? (What is the use of inheritance? What is the advantage of inheritance?) How to use inheritance? When, when, where is the inheritance? If not use inheritance, can you achieve the same Design, if you can distinguish between two scenarios? What is the ability to inherit is a verb, relatively and interface, understanding that the ability to define a class that is defined as a class-based class. Detective class inherit and expand the properties, methods, and events of the base class. Detective classes can also use new ways to rewrite inheritance. Why? Inherit allows you to write only and debug the class once, then the code is continually reused by the foundation of the new class. Inheritance also allows you to use inheritance-based "polymorphism", which is the ability to define as follows: These classes can be exchanged by the client code during runtime, but have the same functionality or attributes. By using inheritance, you can realize the higher level of the same object, the higher level of the father and child relationship object reuse how? Base class and derived class in the derived class by using the inherits keyword to implement the inheritance of the specified base class to the specified base class, the class be labeled as Notinheritable, can not be Inheriting Mustinherit defines an abstract class, the base class cannot be directly instantiated, can only be used to inherit OVERRIDABLE: The corresponding method in the base class can be rewritten Overrides: In the derived class, it means that the class named in the base class is Rewrote NOTOVERRIDABLE: In the base class, the derived class cannot be rewritten to the method: indicating that the derived class must rewrite the method to be used to use mybase: Follow the base class member in the derive class, can not reference the private member can't Use MyBase to call the Mustoverride base class. If the base class is concentrated in different assemblies, you cannot use MyBase to access the base class member marked as Friend. Myclass: The keyword enables you to call the Overridable method implemented in the class and make sure that the implementation of this method is called instead of calling the derived class.
Myclass references contain classes and their inheritance members. MyClass can be used as a modifier of Shared members. MyClass cannot be used in standard modules. MyClass can be used to define such a method, which is defined in the base class but does not provide the implementation of the method in this class. The meaning of this reference is the same as mybase.method.
VB.NET can only achieve single inheritance, that is, there is only one base class in a derived class, but multiple interfaces can be implemented, through the implementation of the interface, can achieve similar purposes. To prevent acceptance in the public base class Limits, the access type of derived class must be more limited to its base class or more than its base class. For example, the PUBLIC class cannot inherit the Friend or Private class, and the Friend class cannot inherit the Private class. WHEN, WHERE? Inherit is a good choice, when:
The inheritance hierarchy represents the relationship of "belonging" rather than "has". You can reuse the code of the base class. The same classes and methods are needed to apply to different data types. The class hierarchy is quite shallow, and other developers are impossible to add too many levels. You need to make global changes by changing the base class.
Another reason for the use inheritance of the base class and code reuse is the advantage of code reuse. A well-designed class can only be debugged once, and then use it as a new class basis. An example of a common valid code reuse is related to the library of managing data structures. For example, assume that there is a large business application that manages several lists in several memory. A list is a copy of the memory of the client database, and is read from the database when the speed is started at the beginning of the session. Similar data structures may be shown as follows: Class CustomerInfo Public PreviousCustomer As CustomerInfo Public NextCustomer As CustomerInfo Public ID As Integer Public FullName As String Function InsertCustomer As CustomerInfo '. Add code to add a CustomerInfo item to the list End Function Function DeleteCustomer As CustomerInfo' Add Code to Remove A Customerinfo Item from the list. End FunctionFunction GetnextCustomer As Customerinfo 'Add CODE GET The Next Customerinfo Item from the list. End Function
Function GetPrevCustomer As CustomerInfo 'Add Code To Get The Previous Customerinfo Item from the list. End FunctionEnd Class application may have a similar product list, which is used in the shopping cart list, as in the code snippet below FIG:. Class ShoppingCartItem Public PreviousItem As ShoppingCartItem Public NextItem As ShoppingCartItem Public ProductCode As Integer Function GetNextItem As ShoppingCartItem 'Add code to get the next ShoppingCartItem from the list End FunctionEnd Class herein can see a pattern: two identical behavior list (Insert, delete and retrieve), but operate different data types. Maintaining two code bases with basic implementation of the same feature is self-satisfied. The most effective solution is to manage the list management in its own class, then inherit from this class for different data types: Class ListItem Public PreviousItem as ListItem Public NextItem () AS ListItem 'Add Code To Get the next item in the list. End Function Function InsertNextItem As ListItem 'Add code to add a item to the list. End Function Function DeleteNextItem As ListItem' Add code to remove a item from the list. End Function
Function GetPrevitem As ListItem 'Add Code To Get The Previous Item from the list. End Functionend ClassListItem class only needs to be debugged. You can then generate classes that use it, but never have to consider list management. For example: Class Customerinfo Inherits ListItem Public ID As INTEGER PUBLIC FULLNAME AS STRINGEND CLASSCLIC PUBPINGCARTITEM inherits ListInd GeneEnd Class Class Class