Tutorials to .com

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

Microsoft. NET's base class inheritance (1)

Print View , by: iSee ,Total views: 34 ,Word Count: 3045 ,Date: Tue, 21 Apr 2009 Time: 7:19 PM

Microsoft. NET inheritance of base class upgrade to Microsoft. NET
Paul D. Sheriff
PDSA, Inc.
December 2001

Abstract: This paper introduces inheritance, how to inherit the base class, and introduced the Microsoft. NET in the realization of the inheritance and interface inheritance.

Objectives outlined in the succession to inherit the base class to learn how to understand interface inheritance prerequisite for understanding the succession to achieve a thorough understanding of the content of this article, we have to satisfy the following conditions:

Understanding of the basic understanding of types of coding and its working principle, or read Creating Classes in. NET (English) a text can use Microsoft ® Visual Basic ®. NET
Directory of succession to inherit the base class to build an overview of examples of forms to create subclasses to add other features
Abstract class MyBase keywords to be used to choose the type of inheritance
Visual Basic 6.0 has been the succession of the new features summary overview of object-oriented programming (OOP) language is one of the principal functions of the "inheritance." Refers to the succession of such a capability: it can use all the features of the existing category, and the need to rewrite the original category of cases for the expansion of these functions. In Microsoft ® Visual Basic ®. NET release, Visual Basic programmers do not have this capacity. In Visual Basic. NET, you can inherit Microsoft. NET framework classes, you can create your own inherited class. In this paper, we will learn how to use inheritance, and to understand how the succession of programming time significantly shortened.

When you create a simple example of the many categories, you will find that you often need to create a class with the previous properties and methods of the same attributes and methods. For example, if there is a category named Person of the base class, such that contains attributes FirstName and LastName and Print method, you will find the Employee category you need these attributes and methods. You may also need other attributes, such as the EmployeeID and Salary. If from the Person class (base class) to inherit, you can add these attributes to a new Employee class, and still be able to access all of the Person class attributes. Refers to a type of succession can be defined as its own with a specific type of all properties and methods, and then by adding other properties and methods defined on the base class's ability to expand.

Terms of succession in-depth study of the subject, let us first define a few terms. Through inheritance to create a new category called "sub-class", was to inherit the class known as the "base class", "parent" or "super-category." In some oop languages, a subclass can inherit multiple base classes. In other words, if there is a Person class and a Car class, the Driver class can inherit all of these two types of attributes and methods. In. NET only allows single inheritance, each subclass can only have one base class.

. NET support for three types of inheritance: The inheritance, interface inheritance and visual inheritance. The realization of the succession is the base class to use the properties and methods without additional coding capacity; interface inheritance is the only properties and methods of use of the name, but the subclass must provide the ability to achieve; visual form inheritance is the child (category) the use of the base form (type) to achieve the look and the ability of the code.

In. NET, a class can inherit from a base class from this base class can inherit from a class from another. In addition, you can use a class in one or more interfaces.

The reasons for the use of inheritance to avoid duplicate preparation of the succession of the same code, so very useful. If there are two separate categories, each category must be the achievement of FirstName and LastName properties, then there may be duplication of code. If you want to change the way the realization of a property, you need to find these attributes have been achieved in all categories in order to make changes. This is not only time consuming, but also an increase of different types in the risk of error.

In considering the use of succession, there is one thing to note, that is, the relationship between the two categories should be "are" relations. For example, Employee is a person, Manager is a human being, therefore these two categories can inherit Person type. Leg category but can not inherit Person type, because the legs are not a person.

Coverage inherited from the base class function, you may find in the base class methods in the preparation of the general category only required for the implementation of the successor function. To perform all the functions required, you can cover in the new category in the base class method without the use of a new name to create a new approach.

Cover, you can choose to complete coverage of the base class methods can also type in succession to write code to perform certain operations, and then call the base class method. When in coverage, be sure to still use the same method as the original contract (the parameters and return type). Can also choose to call the base class method, and then finished in the implementation of base class methods of the preparation of other code.

Inherited base class inheritance allows you to use in a class all of the other types of properties and methods. You can use the Inherits keyword to obtain the functions of base class without having to type the code from a copy and paste to another class.

In this paper, the realization of the succession to create a new category LineDelim, it will inherit Creating Classes in. NET (English) text to create a category of all the features of Line. After, this article by adding two other properties and a way to expand the category of the Line. To add the first attribute is the Delimiter, use it to get a separator characters, and set it to the class. This separator line will be used to replace all space characters for the separator. To add a second attribute is OriginalLine, the text will be used to insert a new line before the separator line to retain the original text. To create a new method is ReplaceAll (), it will be used for the text line to replace all space characters as separators. And then we will learn how to cover GetWord methods in order to use this separator (not spaces) to separate text lines and the first word search.

Construction of sample form shown in Figure 1 will be used to form the test sample to create a type of succession.



Figure 1: Examples of succession for testing form

To create the form shown in Figure 1, click the project (the project), and then click Add Windows Form (to add Windows Forms).
Will form frmLineTest.vb name and click OK (to determine).
In the form and then create the appropriate controls and set properties, as shown in table 1.
Table 1: The form used to test controls the succession of property value
Label Name Label1
Text Line of Text
TextBox Name txtLine
Text The rain in Spain stays mainly in the plain
TextBox Name txtDelim
Text,
GroupBox Name fraWord
Text Get First Word
CommandButton Name btnFirst
Text Get Word
TextBox Name txtFirstWord
Text
ReadOnly True
CommandButton Name btnReplace
Text Replace
TextBox Name txtReplace
Text
ReadOnly True


Construction of Line Class

Next to the Line of succession to build a category.

From the menu, click Project (project), and then click Add Class (add category).
Type the code shown below.
Public Class Line
Private mstrLine As String

Property Line () As String
Get
Return mstrLine
End Get
Set (ByVal Value As String)
mstrLine = Value
End Set
End Property

ReadOnly Property Length () As Integer
Get
Return mstrLine.Length
End Get
End Property

Public Function GetWord () As String
Dim astrWords () As String

astrWords = mstrLine.Split ( "". ToCharArray ())

Return astrWords (0)
End Function
End Class

Since the creation of subclass and base class form has been created, and now can start the implementation inherited.

Click the Project (the project), and then click Add Class (add category). LineDelim.vb the category name and click OK (to determine).
Add a new category, please amend the Visual Basic. NET code created bring it into line with the following sample code is similar.
Public Class LineDelim
Inherits Line

End Class
Inherits Line because of statements, so you can create in this new category of all categories of use Line properties and methods.

FrmLineTest.vb try to open the form.
Double-click Get Word (Word) button.
Click the button to the event procedure, add the following code:
Protected Sub btnFirst_Click (ByVal sender As Object, _
ByVal e As System.EventArgs) Handles btnFirst.Click
Dim oLine As LineDelim = New LineDelim ()

oLine.Line = txtLine.Text
txtFirstWord.Text = oLine.GetWord ()
End Sub
Operation of the project, and the form, click Get Word (Word) button. You will see "The" word appears in the button next to the read-only text box.
Inherits statement is very powerful, only need to use this statement, you can use the Line class LineDelim all types of properties and methods. Despite the implementation of this new type has not yet been any new operation, but it shows that class from the Line of succession to all the code can work properly.

Now add additional features, you can use other attributes and methods to expand LineDelim category. LineDelim would like to add two new types of attributes, follow these steps.

Added in the last part after the Inherits statement to add the two Private variables, as shown below.
Private mstrDelim As String = ""
Private mstrOriginal As String
Type the following code, for the two Private variables of the Property to add the appropriate statement. You can enter the following code on the above two lines of code behind (next two lines).
Public Property Delimiter () As String
Get
Return mstrDelim
End Get
Set (ByVal Value As String)
mstrDelim = Value
End Set
End Property

Public ReadOnly Property OriginalLine () As String
Get
Return mstrOriginal
End Get
End Property
Now you can use the Delimiter property and access to Private variables mstrDelim value.

If you do not want other people to change these attributes, you can set to read-only attribute. To do this, please use the Set statement is no longer, and add in the Property statement ReadOnly property. Of the sample, see the above code to display the attributes OriginalLine statement.

Next, you need to create a method called ReplaceAll, this method can be the text line to replace all the spaces for the transfer of the property Delimiter character separator.

Public Function ReplaceAll () As String
mstrOriginal = MyBase.Line

Return MyBase.Line.Replace ( "", mstrDelim.ToChar ())
End Function
Methods ReplaceAll base class methods of retrieval of the original text Line line. Previously retrieved from the base class attribute is used MyBase.Line syntax. ReplaceAll function of the value attribute MyBase.Line Add as such you just created in the Private variable mstrOriginal. String data type string Replace method to replace all instances of characters in the Delimiter property to set up a new separator characters mstrDelim.

MyBase keyword can be any subclass MyBase keywords used to call the base class of any property or method. Even if the base class method in the subclass had been covered, you can also use the keyword call them. For example, if the base class ReplaceAll exist, but in the sub-class of the method has been covered, you can subclass the ReplaceAll method of ReplaceAll call base class methods.

FrmLineTest.vb try to open the form.
Double-click Replace (replace) to tune out the click event procedure.
Click the button in btnReplace the preparation of the incident the following code:
Protected Sub btnReplace_Click (_
ByVal sender As Object, _
ByVal e As System.EventArgs) Handles btnReplace.Click
Dim oLine As LineDelim = New LineDelim ()

oLine.Delimiter = txtDelim.Text
oLine.Line = txtLine.Text
txtReplace.Text = oLine.ReplaceAll ()
End Sub
This code will Delimiter property is set to form in the sample text box txtDelimiter value. ReplaceAll and then you can call methods, the text line changes all the spaces for the new delimiter character.

Press F5 key to run the project.
Click Replace (replace). You will see that button next to this text box, each sentence has a comma between the words.
Add Delimiter property coverage method, you may want to change the GetWord class LineDelim methods in order to use the appropriate type of separator used to replace a single Line space. Because you do not necessarily want to change the base class, so it is necessary to cover GetWord class method LineDelim function. LineDelim class in the creation of new methods GetWord before, you'll need to type in the Line of GetWord method to add a keyword statement.

In the Solution Explorer (Solution Explorer) window, open the code window Line.vb category.
GetWord method to find the statement (the statement does not contain parameters), as follows:
Public Overloads Function GetWord () As String
Add a statement in the function keyword Overridable, as follows (do not have this keyword, you can not override this method).
Public Overridable Overloads Function GetWord () As String
LineDelim.vb open category, and use the following code to add a new method GetWord.
Public Overloads Overrides Function GetWord () As String
Dim astrWords () As String

astrWords = MyBase.Line.Split (mstrDelim.ToCharArray ())

Return astrWords (0)
End Function
If you want to change in the method of base class function, it may be necessary to add in the function declaration Overrides keyword. Now, LineDelim category GetWord methods you can use the value of property Delimiter to separate the word sentence.

If only one of GetWord coverage process, then the code can only see a version of the method, and can not call the other version of GetWord method. To show all the methods, you must cover every method, just as you type in the LineDelim do the same.

Try by the F5 key to run the project.
Each word in the sentence, enter a comma between and Delimiter (delimited) text box, enter a comma.
Click Get Word (Word).
Sentence of the first word will appear in the text box next to the button.

Abstract class in this part of the example, we learn how to create a Person object, it is because we want to deal with ordinary people. However, you may find, if not the first to add some specific behavior and / or data can not be used on Class Person to take any action. Person so you can type into an abstract class, abstract class definition will only create a subclass of the general properties and methods.

Person category will only be defined as abstract class inheritance, rather than the actual run-time objects created. Inherited from each of these categories (such as the Employee category) will be the use of specific functions to create all the corresponding attributes and methods. For example, Employee Class will create the actual Print method, but only the definition of Person category Print method must exist; Person class is not associated with the Print method of the code.

The use of abstract class due to a combination of. Subclass for the mandatory application designers usually required to achieve all the interfaces, abstract class very useful. You can not undermine the client application to the case method to add a new sub-category, this is by using the interface can not be realized; can be provided in the base class methods to achieve a number of default, thereby reducing the need to complete the sub-class workload.

Succession to create abstract class interface, use the keyword Interface rather than Class. For the interface name, and then the realization of the definition of the need to subclass all the attributes and methods. This is because the base class can be achieved there is no properties and methods, it contains only general information, and does not contain methods. You create a contract, which stipulates that all use this interface subclasses must follow certain rules.


.Net Object Oriented Programming Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.