Inheritance of components
The previous implementation of the C # component combination into the VB project is just a cow knife, now we will implement the Object-oriented Programming (OOP) of the hybrid language, first we try to mix the inheritance of the hybrid language components.
Create a VB Class library project vbcomponent, add a reference to the CSHARPCLASS.DLL component in the project, then add a new class in the component: ExtendsfromCSharp, which inherits from the CSHARPCLASS, and the ExtendsFromCSharp class provides an Add (x, Y) method, add two additions, use UML as shown in Figure 4:
Figure 4 Inherited VB class from C # class
The specific code is as follows:
Imports csharpclassnamespace.csharpclass
'Inheriting class csharpclass from C # components
Public Class ExtendsfromCSharp
Inherits csharpclassnamespace.csharpclass
Public Function Add (Byval x as integer, Byval Y as integer) AS Long
Return X Y
END FUNCTION
END CLASS
Compilation Generate VBComponent.dll components (as for how to use and test this class, don't have to say more?).
Now, the ExtendsFromCsharp class in the VBComponent component will have two ways: Saysomething from the C # base class CSharpClass, and the add () function is implemented by the VB. The EXTENDSFROMCSHARP class object you created actually combined two different language development features. You don't even know which language is developed by these methods and functions. Is it very magical?
Let us take a break and make a summary:
Now we have mastered the way to derive new categories from the ready-made components, this is to say that we can build our own component library and dynamically expand it when appropriate, this extension is cross-language.
When using a mixed language component in VS.NET, when the components used are modified, they should be done dynamically update the project reference. The method is to regenerate the solution, or re-join the project after deleting the original reference.
Since the components are mutually inherited, this has caused dependence between component libraries (such as csharpclass.dll), so in actual project development, the component library developed by any project team member, They are required to declare their component library dependence in the document. In addition, when the system development completes the deployment to the user computer, the interdependent component library must be tied together to copy to the user's computer, and only one is available.
Below, let us go further, realize polymorphism between components!
Polymorphism between components
Let's implement a "classic" polymorphism example, which describes the fact that the circles and rectangles are geometric patterns, they can ask for an area.
Continue the above example, select the original CSHARPCLASS.CS file in the C # project, enter the following code outside of class csharpclass:
Public Interface IMYSHAPE
{
Double area ();
}
Public Class CsharPRect: IMYSHAPE
{
Public float width;
Public float height;
Public Double Area ()
{
Return Width * Height;
}
Obviously, here we define an interface IMYSHAPE, class CsharPrect implements this interface, which provides its own implementation of the function Double Area () defined by the interface:
Rectangular area = length * wide
Compilation generation solution, csharpclass.dll is updated.
Open the VBComponent project, notice that this project is refreshed with CSHARPCLASS.DLL, check the method is in the selection menu: View à object browser, see if CsharpClass reflects new changes, such as 5 chart:
Figure 5 Check if the component is updated
Create a category file vbircle.vb, enter the following code:
Public class vbcircle
Implements CsharpclassNamespace.Imyshape
Private _Radius As Single
Property Radius () AS Single
Get
RADIUS = _radius
END GET
Set (byval value as single)
_Radius = Value
End set
End Property
Public Sub New ()
_Radius = 0
End Sub
Public Function Area () As Double Implements CsharpclassNameSpace.Imyshape.area
Return System.math.pi * _Radius * _Radius
END FUNCTION
END CLASS
Compile, generate vbcomponent.dll.
Now, we realize the design of components-based object system design:
Create a Windows application using VB, add a reference to the above two component libraries in the project, add a button on the form, and write the following code in its Click event:
Private sub button_click (...) Handles Button6.click
DIM VBOBJ AS New VBComponent.vbcircle ()
Vbobj.radius = 10
DIM CSHARPOBJ AS New CsharpclassNamespace.csharPRect ()
DIM Area as Double
Csharpobj.width = 10
Csharpobj.height = 20
Calarea (VBOBJ)
Calarea (Csharpobj)
End Sub
Where Calarea () is a SUB process, which receives a parameter of a IMYSHAPE type, which can be passed to any of the objects that implement the IMYSHAPE interface, which is a typical polymorphism. Encoding.
Private Sub Calarea (Byval Obj As CsharpclassNameSpace.imyshape)
Messagebox.show (Convert.ToString (Obj.Area ())))
End Sub
After testing, cross-language, cross-assemblies can indeed move normally.
Ok, now let us think about what is achieved, how can we apply it to development practices:
We can package an interface into a component, then implement this interface. We can put it in different components, and give different programmers to develop in their favorite language. Programmers using these components As long as the interface variables are always used instead of specific classes in his code, we can dynamically replace a specific implementation interface, without affecting the programs that use these components. Wow, this doesn't mean we can make a complete software system to get eight pieces, then select a component insertion system according to the specific situation? awesome! Below, let us start the most exciting tours of the component-programming - the dynamic assembly and swap of the software components.