When he handed the homework to the art teacher, one of the students handed in a blank sheet of paper.
The teacher asked: "Where's the painting?" ”
Student answer: "Here?" He said, pointing to the white paper.
Teacher: "What are you drawing?" ”
Student: "Cow eats grass." ”
Teacher: "Where's the grass?" ”
Student: "The cows have eaten up." ”
Teacher: "What about cows?" ”
Student: "The grass has eaten up, what are the cows standing there for?" ”
First, abstract base class
Just as there is a good father can struggle less than 21, the parent class itself is very useful, can be instantiated, can not be instantiated, do a look, the benefits are great.
1, Mustinheit
A base class can be instantiated, but it can also not instantiate (or prohibit the creation of objects of that class), and only subclasses, as well as objects of subclasses, are created.
You can use MustInherit to indicate that the base class cannot create an object.
Note: This does not mean that you cannot declare a variable of the base class (the following person) type, except that you cannot create an object with new Peron.
At the same time, we can continue to use the shared approach (because it belongs to the class, not the object)
2, Mustoveride
Indicates methods (Sub,funciton,property) that can only be overridden by subclasses, which have only method headers and no specific implementation code. Therefore, you cannot create an object.
Therefore: When a class contains Mustoveride, this class must be declared by MustInherit. That
You can use MustOverride only in property and procedure declaration statements. The property or procedure that specifies MustOverride must be a member of the class, and the class must be marked
Remember as MustInherit.
Public MustInherit Class person ' can only be inherited private mname as String private mbirthdate as Date private MID as Strin G Public Event namechanged (ByVal newName As String) public event datachanged (ByVal field As String, ByVal newvalue As Object) public MustOverride Function lifeexpectancy () as Integer ' no implementation code, only quilt class rewrite ' ... End Class
The above declaration Mustoveride function Lifeexpectancy only method header, no implementation code, such method is also called "abstract method" or "pure virtual function"
Any subclass that inherits this class must override these abstract methods, otherwise a syntax error will occur and the subclass will not compile.
Therefore, add the following subclass:
The pure virtual function of overriding base class in ' ======= Subclass ' employee =============== public Overrides function lifeexpectancy () as Integer Return 90 End Function
3. Abstract base class
MustInherit and MustOverride when the class is present, this class is called an abstract base class. Or, a class that contains abstract methods is called an abstract base class.
Abstract base classes are useful, you can only frame on the early stage of the abstract base class this shelf, each sub-class according to their own situation to rewrite the appropriate method.
Note that at this point the subclass must have an implementation code for the abstract method, otherwise an error occurs.
Public MustInherit Class Abstractbaseclass public MustOverride Sub DoSomething () Public MustOverride Sub Doothersomething () End Class ' interface public Interface Iabstractbaseclass Sub DoSomething () Sub Doothersomething () End Interface
In some respects, the abstract base class is similar to the interface interface concept, the above interface is Iabstractbaseclass, and there must be two methods in the class that implements the interface.
Implementation code, or else it will be an error.
4. Prohibition of inheritance
Above is the object created in "Inherit only".
The following is "prohibition of inheritance", with notineritable to explain this class, no more inheritance, so far!
Public NotInheritable class Officeemployee ' Specifies that the base is no longer a base class (that is, it is no longer derived)
Classes that cannot be inherited are sometimes referred to as "sealed" classes
Second, multi-interface
Interface, like a diplomat, can communicate with the outside world.
In vb.net, an object can have one or more interfaces. All objects have a primary or local interface, and the interface is represented by methods, properties, events, and fields declared with public.
In addition to the local interface, an object can also use implements to implement a secondary interface.
1. Object interface
The class can be used as a local interface in addition to the private declaration. Cases:
' method as interface public Sub amethod () End Sub ' attribute as interface public Property Aproperty () as String End ' event events as interface Public Event aevent () ' variable as interface public Ainteger as Integer
Note: There is no implementation code above, because the interface is only the declaration of the Shelf, its implementation code is not part of the interface. This is the important difference!
Because it is the core content of object-oriented programming to distinguish interface from implementation code.
using the local interface
Once the local interface is declared, it is possible to communicate with the outside world using these interfaces in the object. The Pulbic method in the following class, whose object can be directly associated with the outside world.
The local interface of the ' ============ class =========public class Theclass public Sub dosomething () ' ... End Sub Public Sub doothersomething () ' ... . End subend Class ' ========== use both interfaces in the main program ======== Dim myObject as New theclass myobject.dosomething () Myobject.doothersomething ()
2. Auxiliary interface
In the design, only the same kind of objects have the same local interface, which is a great limitation.
If you put a different set of objects, their local interface is different, the processing is troublesome.
For example, for a product, customer, invoice three class object, does not have a natural inheritance relationship, but we need to print these three information, so,
Produced another concept of "auxiliary interface", also on the usual interface, through this interface, so that all three can be printed.
Interface interface is the same level of concept as Class,modul, it does not have implementation code like an abstract method, the implementation code is using the interface
In the class. When you use an interface, you do not have to be in the same type of object to work correctly.
(1) Defining the interface
Define an interface with interface and add the interface code outside the module by adding a module:
Public Interface Iprintableobject ' interface definition (same as Class,module peer) internal scope same function Label (ByVal index as Integer) as String ' So it has no public,friend, such as the limit Function Value (ByVal index as Integer) as String ReadOnly Property Count () as Integerend Int Erfacemodule Interfacesend Module
The interface definition is contained within the Interface and End Interface statements.
After the Interface statement, you can choose to add a Inherits statement that lists one or more inherited interfaces. That is, the interface can inherit.
In the declaration, the Inherits statement must be present before all other statements except the comment.
The rest of the statements in the interface definition should include the Event, Sub, Function, property, Interface, Class, Structure, and Enum statements.
An interface cannot contain any implementation code or statements associated with implementing code, such as end Sub or End property.
In a namespace, by default, an interface statement is Friend, but it can also be explicitly declared as public or friend.
Interfaces defined in classes, modules, interfaces, and structs default to public, but can also be explicitly declared as public, Friend, Protected, or Private.
The Shadows keyword can be applied to all interface members. The Overloads keyword can be applied to Sub, Function, and property statements declared in the interface definition.
In addition, the property statement can have a Default, ReadOnly, or WriteOnly modifier. No other modifiers are allowed: public, Private,
Friend, Protected, Gkfx, Overrides, MustOverride, or Overridable.
An interface is a data type, similar to a class or struct (struction). Because this type of variable can be defined like this:
Private printable as Iprintableobject
But above, there is no implementation of code, implementation code is implemented in the class. So the interface cannot be used directly with new.
(2) using the interface
Users only need to know the interface, they can use different objects without having to know the details of the object.
Add a method to the form:
In the public Sub printobject (obj as Iprintableobject) ' form, add the routine Dim index as Integer for index = 0 to obj. Count debug.write (obj. Label (Index) & ":") Debug.WriteLine (obj. Value (index)) Next End Sub
Then add the button to the form and add the code:
Private Sub Buttonprint_click (sender as object,e as EventArgs) Handles Buttonprint.click Dim obj as New Employee ()
obj. EmployeeNumber = 123 obj. BirthDate = #1/1/1980# obj. HireDate = #1/1/1996# printobject (obj) End Sub
Clicking Run will prompt the error: Because the interface is just a method header or event header, there is no specific implementation code. Therefore, you also add the implemented code to the class.
(3) Implement the interface
Any class (except the abstract base class) can use implements to implement an interface.
Because the interface is to be passed with the object of the class, the interface is finally Yanzhiyouwu. Abstract base classes cannot produce objects, so abstract base classes cannot have interfaces.
After adding implements carriage return, the shelves of the interface methods or events are automatically added to the class, and we only need to add the implementation code inside.
The name of the method, property, etc. implemented in the class is not important, and it is important that the data type of the parameter and the return value must match the type of the interface definition.
The implementation code is as follows:
Implement interface in ' ================employee class (Connect to external interface type) ========= Implements iprintableobject ' here press ENTER, Automatically add iprintableobject members to implement details ' for interface preparation or fields to use Private mlabels () as String = {"ID", "Age", "HireDate"} Private mbirthdate as Date private msalary as Double public ReadOnly Property Count as Integer Implements iprintab Leobject.count Get Return UBound (mlabels) End Get End Property Private Function Label (index as Integer) as String Implements Iprintableobject.label Return mlabels (index) End Function Public Function Value (index as Integer) as String Implements iprintableobject.value Select Case index case 0 Return CStr (employeenumber) Case 1 return CStr (age) Case Else return Format (HireDate, "Short Date ") End Select End Function
Note: You can see that the implementation method is followed by implements Iprintableobject.label, which is similar to handles, and it can also be used by commas to
Multiple methods of an interface are linked to a method, or methods of multiple interfaces are contacted in a method.
(In fact, the function address is associated with the outside world, so that the use of this address, naturally called the method in different objects)
Look at the entire interface:
(4) Reuse of common implementation code
Auxiliary interface, can have several, several can the same method, this method becomes the common method.
The following two interfaces have methods that are linked to the same method value (in class Employe)
' ========= interface 1, with Value=================public Interface iprintableobject Function Label (ByVal index as Integer) as String Function Value (ByVal index as Integer) as String ReadOnly Property Count () as Integerend Interface ' ======= = Interface 2, with Getvalue===============public Interface ivales Function GetValue (ByVal index as Integer) as Stringend Interface ' =======employee two interfaces, which are associated to the value function (the function signature and the same parameters above) =========== Implements iprintableobject Implements ivales public Function Value (index as Integer) as String Implements Iprintableobject.value, Ivales.getvalue Select Case index case 0 return CStr (employeenumber) Case 1 return CStr (age) Case Else Return Format (HireDate, "Short date") end Select End Function
As you can see, it is followed by a comma, and two interfaces are used with the value method.
(5) Merging interfaces, inheritance
You can merge both the secondary interface and the inheritance in the.
When inheriting from a class that implements an interface, the new subclass automatically obtains the implementation code for the interface and base class.
Subclasses can override these methods if the specified base class method can be overridden.
Not only the implementation code of the base class master interface is overridden, but also the implementation code of the interface can be overridden.
Public Overridable Function Value (ByVal index as Integer) as String Implements Iprintableobject.value, Ivalues.getvalue
The above indicates that this interface is implemented and can be rewritten.
The complete definition is as follows:
Public Overridable Function Value (index as Integer) as String Implements Iprintableobject.value, Ivales.getvalue Select Case index Case 0 return CStr (employeenumber) Case 1 return CStr (age) Case Else return Format (HireDate, "Short date") end Select End Function
Note: Because the above is public, it is also valid in the main interface (local interface).
And it's also part of two interfaces, meaning it can be accessed in three ways.
Three different ways to access:
Dim emp As New Employee ("Andy") Dim printable As Iprintableobject = emp Dim values As Ivales = EMP debug.write Line (EMP. Value (0)) debug.writeline (printable. Value (0)) Debug.WriteLine (values. GetValue (0))
First Type: Primary interface (local interface)
Second type: Iprintableobject interface access
The third type: Ivalues interface access
Such three interfaces are merged in one, all in the value of this method.
To declare an implementation of an interface method, you can use any of the attributes that are valid on the instance method declaration (including Overloads, Overrides, Overridable, Public, Private, Protected, Friend, Protected Friend, MustOverride, Default, and Static).
The Shared attribute is not legal because it defines a class rather than an instance method.
--------------------------------------------------------------------------------------------------------------- --------
Reproduced in: http://blog.csdn.net/dzweather/article/details/9966159
Vb MustOverride MustInherit