Base class inheritance in Microsoft. NET (2)

Source: Internet
Author: User
Tags define definition class definition inheritance new features visual studio
Now, add a new class to the project that you have created.
From the Visual Studio menu, click Project (Project), and then click Add Class (Add classes).
Add the following code to the class:
Interface person
Property FirstName () as String
Property LastName () as String
Sub Print ()
Sub Talk ()
End Interface
You will find that you define properties and subroutines in the same way that you typically define these properties and procedures. The only difference is that you don't write any code for them. Now let's see how this interface is used in class definitions.

Add the following code to the class file that you created in the previous step:
Public Class Employee
Implements person

Private Mstrfirstname as String
Private Mstrlastname as String

Property FirstName () as String _
Implements Person.firstname
Get
Return Mstrfirstname
End Get
Set
Mstrfirstname = Value
End Set
End Property

Property LastName () as String _
Implements Person.lastname
Get
Return Mstrlastname
End Get
Set
Mstrlastname = Value
End Set
End Property

Sub Print () Implements Person.print
' Add some code here
End Sub

Sub Talk () Implements Person.talk
' Add some code here
End Sub
End Class
The first line after the Employee class definition is Implements person. This keyword indicates that you want to conform to the contract defined in the person interface. You can now define all the properties and methods in the contract. After each property statement, you must include the Implements keyword, and you must specify the name of the interface and the name of the method/property that you are using (there is a point between two names [.] )。 Visual Basic. NET tracks each interface, and you cannot compile the application until all interfaces have been created.

If you want to run the code, you need to create the corresponding child procedure, because in the example above, the child procedures are left blank. After you create all the child procedures, you can declare and use the new Employee object as you would normally create and use any other object.

Select the type of inheritance to use
Sometimes it is very difficult to decide whether to use implementation inheritance or interface inheritance, and in many cases, both types of inheritance may be used, but only a small portion of it is involved. For example, you might want to add a method definition in the line class that must be overridden by a quilt class, and use the MustOverride keyword in the process definition to do so.

Public MustOverride Sub Init ()
After you add this definition to a class, it acts like an interface. In subclasses, you must define the Init method, and the method must use the Overrides keyword. The following is an example of how to define the Init method:

Public Overrides Sub Init ()
Mstrdelim = ""
Mstrline = "Test Line"
End Sub
Also, remember to use the Overrides keyword. This keyword is used to notify the compiler that this method overrides the Init method in the parent class.

Note: the Microsoft. NET Framework Online Help provides a design guide that can help you decide which type of inheritance to use.
Block Inheritance
In some cases, you may not want other classes to inherit your class. If this is the case, you can use the keyword NotInheritable to block inheritance of the class.

Public Class NotInheritable Employee
' Class definition
End Class
New features since Visual Basic 6.0
With Visual Basic. NET, you can inherit all the classes contained in the. NET Framework. You can create your own classes that inherit existing classes, and add or remove functionality by making simple changes to your code.

Summarize
This article describes how to inherit a base class, add additional properties to the base class, and use the Overrides keyword to replace the functionality defined in the base class. It also describes using the MyBase keyword to call methods in the base class to extend the functionality of the base class. Although inheritance does not apply to all applications, inheritance becomes a very powerful tool if used correctly.

About the author
Paul D. Sheriff is the owner of PDSA, Inc. The company, located in South California State, is a custom software development and consulting firm. Paul is the MSDN regional director of Southern California, with Paul Sheriff teaches Visual Basic, who produced more than 70 sets of Keystone Learning Systems on Visual Basic, SQL The video materials for Server,. NET, and Web Development have also recently collaborated with Ken Getz to publish a book on SAMS, titled "ASP.net Jumpstart". For more information, visit the PDSA, Inc. Web site www.pdsa.com.

About Informant Communications Group
Informant Communications Group, Inc. is a multimedia company focused on the information technology industry. It was founded in 1990, specializing in software development publications, conferences, catalog publishing and Web sites and other services. With offices in the United States and the UK, ICG has become a prestigious media and marketing content integrator and has been meeting the growing needs of IT personnel with high quality technical information.




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.