Ref: http://msdn2.microsoft.com/en-us/library/z9k8e08x.aspx
Polymorphism refers to the ability to define multiple classes with functionally different, yet identically named methods or properties that can be used interchangeably by client code at run time.
In This Section:
How Visual Basic Provides Polymorphism Illustrates different approaches to polymorphism.
Inheritance-Based Polymorphism Discusses using inheritance to achieve polymorphism.
Interface-Based Polymorphism Discusses using interfaces to achieve polymorphism.
How Visual Basic Provides Polymorphism?
Traditionally, polymorphism in Visual Basic was done by using interfaces, and interfaces can still be used for this purpose. However, now Visual Basic provides the option of using inheritance to provide polymorphism.
As with other issues in object-oriented programming, the option to use depends on your specific requirements. In general, use inheritance when you want to create baseline functionality that derived classes can extend; use interfaces when similar functionality has to be provided by multiple implementations that have little in common.
Most object-oriented programming systems provide polymorphism through inheritance. Inheritance-based polymorphism involves defining methods in a base class and overriding them with new implementations in derived classes.
For example, you could define a class, BaseTax, that provides baseline functionality for computing sales tax in a state. Classes derived from BaseTax, such as CountyTax or CityTax, could implement methods such as CalculateTax as appropriate.
Polymorphism comes from the fact that you could call the CalculateTax method of an object belonging to any class that derived from BaseTax, without knowing which class the object belonged to.
The TestPoly procedure in the following Visual Basic Code demonstrates inheritance-based polymorphism:
' %5.3 State tax
Const StateRate As Double = 0.053
' %2.8 City tax
Const CityRate As Double = 0.028
Public Class BaseTax
Overridable Function CalculateTax(ByVal Amount As Double) As Double
' Calculate state tax.
Return Amount * StateRate
End FunctionEnd Class
Public Class CityTax
' This method calls a method in the base class
' and modifies the returned value.
Inherits BaseTax
Private BaseAmount As Double
Overrides Function CalculateTax(ByVal Amount As Double) As Double
' Some cities apply a tax to the total cost of purchases,
' including other taxes.
BaseAmount = MyBase.CalculateTax(Amount)
Return CityRate * (BaseAmount + Amount) + BaseAmount
End FunctionEnd Class
Sub TestPoly()
Dim Item1 As New BaseTax
Dim Item2 As New CityTax ' $22.74 normal purchase.
ShowTax(Item1, 22.74) ' $22.74 city purchase.
ShowTax(Item2, 22.74)End Sub
Sub ShowTax(ByVal Item As BaseTax, ByVal SaleAmount As Double)
' Item is declared as BaseTax, but you can
' pass an item of type CityTax instead.
Dim TaxAmount As Double
TaxAmount = Item.CalculateTax(SaleAmount)
MsgBox("The tax is: " & Format(TaxAmount, "C"))
End Sub
In this example, the ShowTax procedure accepts a parameter named Item of type BaseTax, but you can also pass any of the classes derived from the BaseTax class, such as CityTax. The advantage of this design is that you can add new classes derived from the BaseTax class without changing the client code in the ShowTax procedure.
Interface-Based Polymorphism
Interfaces provide another way you can accomplish polymorphism in Visual Basic. Interfaces describe properties and methods like classes, but unlike classes, interfaces cannot provide any implementation. Multiple interfaces have the advantage of allowing systems of software components to evolve without breaking existing code.
To achieve polymorphism with interfaces, you implement an interface in different ways in several classes. Client applications can use either the old or the new implementations in exactly the same way.
The advantage to interface-based polymorphism is that you do not need to re-compile existing client applications to get them to work with new interface implementations.
The following example defines an interface named Shape2 that is implemented in a class named RightTriangleClass2 and RectangleClass2.
A procedure named ProcessShape2 calls the CalculateArea method of instances of RightTriangleClass2 or RectangleClass2:
Sub TestInterface()
Dim RectangleObject2 As New RectangleClass2
Dim RightTriangleObject2 As New RightTriangleClass2
ProcessShape2(RightTriangleObject2, 3, 14)
ProcessShape2(RectangleObject2, 3, 5)
End Sub
Sub ProcessShape2(ByVal Shape2 As Shape2, ByVal X As Double, ByVal Y As Double)
MsgBox("The area of the object is " & Shape2.CalculateArea(X, Y))End Sub
Public Interface Shape2
Function CalculateArea(ByVal X As Double, ByVal Y As Double) As Double
End Interface
Public Class RightTriangleClass2
Implements Shape2
Function CalculateArea(ByVal X As Double, ByVal Y As Double) As Double
Implements Shape2.CalculateArea ' Calculate the area of a right triangle.
Return 0.5 * (X * Y)
End Function
End Class
Public Class RectangleClass2
Implements Shape2
Function CalculateArea(ByVal X As Double, ByVal Y As Double) As Double
Implements Shape2.CalculateArea ' Calculate the area of a rectangle.
Return X * Y
End Function
End Class
No comments:
Post a Comment