Tutorials to .com

Tutorials to .com » Dotnet » Oop » Microsoft. NET's base class inheritance (2)

Microsoft. NET's base class inheritance (2)

Print View , by: iSee ,Total views: 15 ,Word Count: 1061 ,Date: Tue, 21 Apr 2009 Time: 6:49 PM

Now, the project has been created in the Add a new category.
From the Visual Studio menu, click Project (project), and then click Add Class (add category).
In the class add the following code:
interface Person
Property FirstName () As String
Property LastName () As String
Sub Print ()
Sub Talk ()
End Interface
You will find that you define the attributes and methods of sub-processes and you are the definition of these attributes and processes the same way. The only difference is that you do not have any code to prepare for them. Now take a look at the definition of how to use this type of interface.

In the previous steps to create the class file to add the following code:
Public Class Employee
Implements Person

Private mstrFirstName As String
Private mstrLastName As String

Property FirstName () As String _
Implements Person.FirstName
Return mstrFirstName
End Get
mstrFirstName = Value
End Set
End Property

Property LastName () As String _
Implements Person.LastName
Return mstrLastName
End Get
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
In the Employee class definition is the first line after Implements Person. This keyword indicates that you have to comply with the definition of Person of the contract interface. Now you can define in the contract all the attributes and methods. Statement in the back of each Property, Implements keyword must be included, and must specify the interface name and you are using the method / property name (both the name of a point between the [.])。 Visual Basic. NET keeps track of each interface, all interface created, you can not compile the application.

If you want to run the code, you need to create a corresponding sub-process, because in the example above, these sub-process is blank. Create all sub-processes, you can create and you usually use, like any other object, the statement and the use of a new Employee object.

Choose to use the type of inheritance is sometimes difficult in the end decided to use interface to achieve the use of inheritance or succession, in many cases, it may be two types of inheritance will be used, but only a small part. For example, you may need to add in the Line type must be covered by the method of quilt-type definition, the definition in the process can be realized using the keyword MustOverride this operation.

Public MustOverride Sub Init ()
Add to this the definition of class, its role is similar to an interface. In sub-class, the Init method must be defined and the method must use the Overrides keyword. The following is a method of how to define Init Example:

Public Overrides Sub Init ()
mstrDelim = ""
mstrLine = "test line"
End Sub
Similarly, remember to use the Overrides keyword. The keywords used to inform the compiler will cover this method in the superclass Init method.

Note: Microsoft. NET framework to provide online help in the design guidelines that can help you decide to use the type of inheritance.
To prevent the succession, in some cases, you may not want others to inherit your class. If so, you can use the keyword NotInheritable to prevent the kind of succession.

Public Class NotInheritable Employee
'Class definition
End Class
Visual Basic 6.0 new features since the use of Visual Basic. NET, you can inherit. NET framework contains all the categories. You can create your own categories, so that these types of inheritance of existing classes; and through a simple code change to add or remove features.

Summary This article describes how to inherit the base class, base class how to add other attributes, as well as how to use the Overrides keyword to replace in the definition of base class functions. Also introduced the use of keywords MyBase call the base class methods, in order to extend the base class function. Although the succession is not the application of all applicable, but if used correctly, the succession will be a very powerful tool.

About the author
Paul D. Sheriff is the PDSA, Inc.'s Owners. The company is located in Southern California, is a custom software development and consulting firm. Paul is the MSDN Southern California Regional Director, there is the "Paul Sheriff Teaches Visual Basic" book, he Keystone Learning systems has produced more than 70 units on the Visual Basic, SQL Server,. NET and Web development of video materials and, more recently, Ken Getz will be co-publishing a book on the SAMS book, "ASP.NET Jumpstart". For more information, please visit the PDSA, Inc.'s Web site www.pdsa.com.

About Informant Communications Group
Informant Communications Group, Inc. Is a focus on the information technology industry's multi-media companies. It was founded in 1990, specializes in software development publications, conferences, catalog publishing and Web site services. ICG in the United States and the United Kingdom with offices, has become renowned for media and marketing content integrator and high-quality technical information to IT staff to meet growing demand.

.Net Object Oriented Programming Articles

Can't Find What You're Looking For?

Rating: Not yet rated


No comments posted.