VB.net using interface to realize polymorphism

Source: Internet
Author: User

Users can use interfaces in vb.net to complete the implementation of polymorphism. By using multiple interfaces, users can allow system components to run multiple software without disrupting the running of code.
interfaces describe properties and methods like classes, but unlike classes, interfaces do not provide any implementation.
in order to use interfaces for polymorphism, the user must first establish an interface and implement the interface through several other classes. In almost the same way, users can invoke methods that other objects have already implemented.
The following example is a method of using an interface to implement polymorphism:

Namespace polynamespace Interface Animal Sub Move (ByRef Distance as Double) Sub Bite (Byval What as OBJ ECT) End Interface Class Flea Implements Animal public Sub bite (Byval What as Object) Implements
          Al.bite ' Bite something End Sub sub-move (ByRef Distance as Double) Implements Animal.move Distance=distance+1 End Sub-End Class class Dog Implements animal public Sub bite (Byval What As Object) Implements animal.bite ' bite something End Sub sub-move (ByRef Distance as Double) Implements Animal.move distance=distance+100 End Sub-end Class end Namespace ' Add this section to the Your form Pr otected Sub button1_click (Byval sender as System.object,_ Byval e as System.EventArgs) Dim Aflea as New F  Lea () Dim anobj As Object () Dim adog As New Dog () Getfood (aflea,anobj) Getfood (adog,anobj) End Sub Public Sub Getfood (Byval Critter AS animal,byval Food as Object) Dim dbldistance as Double ' Code to calculate distance to Food (omitted). Critter.move (dbldistance) ' Early bound (vtable) critter.bite (Food) ' Early bound (vtable) End Sub

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.