Vb. NET Chinese Tutorial (9) redefining (overriding) program

Source: Internet
Author: User
Tags definition integer
Programs | tutorials | Chinese theme: redefining (overriding) programs


?????????? Content??????????
V 1. Redefining programs








1. Redefining (Override) procedures

In applications, it is common to see that a subclass inherits from a parent class and does not conform to subcategories. At this point you can design a new program to replace it.


Figure 1, redefinition of program members

For example, the,salesperson category contains bonus () Programs;salesmanager bonus () programs inherited by salesperson. Bonus () can calculate the bonuses of the sales staff. However, the general sales staff and sales manager of the bonus calculation method is different, so,salesperson bonus () obviously does not apply to SalesManager. This SalesManager category must define the applicable bonus () to calculate the bonus of the sales manager. Subcategories are not satisfied with the inherited program and are replaced by a custom program, which is referred to as "redefinition" or "redefining" (overriding). The self-defined program is the same as the name of the inherited program. Please look at a program:

' Ex01.bas
Imports System.ComponentModel
Imports System.Drawing
Imports system.winforms
'-----------------------------------------------------------------------------------------
Class Salesperson
Protected TotalSales as Double
Public Sub New (ByVal t as Double)
TotalSales = t
End Sub
Public Overridable Function Bonus () as Double
Bonus = TotalSales * 0.008
End Function
End Class

Class SalesManager
Inherits Salesperson
Public Sub New (ByVal t as Double)
MyBase.New (t)
End Sub
Public Overrides Function Bonus () as Double
Bonus = totalsales * 0.008 + 1000
End Function
End Class
'----------------------------------------------------------------------------------------
Public Class Form1
Inherits System.WinForms.Form

Public Sub New ()
MyBase.New ()
Form1 = Me
' This is required by the Win Form Designer.
InitializeComponent ()
' Todo:add any initialization on the InitializeComponent () call
End Sub
' Form overrides dispose to clean up the component list.
Public Overrides Sub Dispose ()
Mybase.dispose ()
Components. Dispose ()
End Sub
#Region "Windows Form Designer generated code"
.......
#End Region
Protected Sub Form1_Click (ByVal sender as Object, ByVal
e as System.EventArgs)
Dim Jim as New salesperson (50000)
MessageBox.Show ("Jim ' s Bonus:" + str (Jim.bonus ()))
Dim Tom as New SalesManager (45000)
MessageBox.Show ("Tom's Bonus:" + str (Tom.bonus ()))
End Sub
End Class

This program outputs the following:jim ' s bonus:400
Tom ' s bonus:1360

Because Jim is the object of the Salesperson category, Jim.bonus () refers to the salesperson Bonus () program. The SalesManager category inherits this bonus () and defines the new bonus () program yourself. This new program replaces the inherited program. thus,alvin.bonus () refers to the Bonus () program that SalesManager itself defines.
In the process of "redefining" (overriding), the phenomenon of "duplicate definition" (overloading) must also occur. The former is for the "parent-child" category, the subcategory has "fixed" (refinement) or "replace" (replacement), the process of defining the same name to replace the parent class program. As for the definition of repetition, the scope is broader and the definition of the program can be repeated within the category. Duplicate definitions can also occur between parent-child classes, even between separate two categories; After a duplicate definition, the new program and the original program are only similar in meaning (semantic), but not necessarily the same, For example, bonus (ByVal y as Year) and bonus (ByVal y as Employee) have different meanings.
However, redefining a program, for example, in the salesperson example above, the parent class Salesperson.bonus () and subcategory Salesmanager.bonus () all represent the same meaning--the calculation of dividends, but the method is different. As a,"redefine" the process of replacing the parent class with a different execution process, but there is a consistent intent between the old and new programs. " I hope you can distinguish this easily confusing concept. Please look at the program again:

' Ex02.bas
Imports System.ComponentModel
Imports System.Drawing
Imports system.winforms

'--------------------------------------------------------------------------------------
Public Class Person
Private name as String
Private Age as Integer
Public Sub New (ByVal na as String, ByVal A as Integer)
name = NA
Age = A
End Sub
Public Function Birthday () as Integer
Birthday = 2001-age
End Function
Public Overridable Sub Display ()
MessageBox.Show (' name: ' + name + ' Age: ' + str (age))
End Sub
End Class

Public Class Teacher
Inherits person

Private Salary as Decimal
Public Sub New (ByVal na as String, ByVal A as Integer, ByVal sa as Decimal)
MyBase.New (Na, a)
Salary = sa
End Sub
Public Overrides Sub Display ()
Mybase.display ()
MessageBox.Show ("Salary:" + str (Salary))
End Sub
End Class

Public Class Student
Inherits person

Private Student_number as Integer
Public Sub New (ByVal na as String, ByVal A As Integer, ByVal No As Integer)
MyBase.New (Na, a)
Student_number = No
End Sub
Public Overrides Sub Display ()
Mybase.display ()
MessageBox.Show ("Studno:" + str (student_number))
End Sub
End Class
'---------------------------------------------------------------------------------------
Public Class Form1
Inherits System.WinForms.Form

Public Sub New ()
MyBase.New ()

Form1 = Me
' This is required by the Win Form Designer.
InitializeComponent ()
' Todo:add any initialization on the InitializeComponent () call
End Sub
' Form overrides dispose to clean up the component list.
Public Overrides Sub Dispose ()
Mybase.dispose ()
Components. Dispose ()
End Sub
#Region "Windows Form Designer generated code"
.......
#End Region
Protected Sub Form1_Click (ByVal sender as Object, ByVal
e as System.EventArgs)
Dim x as New Teacher ("Linda", 33, 50500)
Dim y as New Student ("Tom", 36, 11138)
X.display ()
Y.display ()
End Sub
End Class


This program outputs:
Name:linda age:33
salary:50500
Name:tom age:36
studno:11138

Person's display () can not display the teacher salary data, so teacher must "amend"person original display () program. " Similarly, the student category also "corrects the original display () Program of"person. N





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.