Vb. NET Chinese Tutorials (3) Inheritance and encapsulation

Source: Internet
Author: User
Tags inheritance integer
Package | inheritance | tutorial | Chinese 1. Inheritance and encapsulation (encapsulation)
1.1 Public and private data

The concept of "seal possession" (encapsulation) has been introduced earlier. That is, the data members defined within the category are restricted to program members for access. Now the question is: can subcategories directly access the data in the parent category? Like: Children inherit property from their parents, but can they take or sell the inherited property? If so, it is clearly against the ideal of "sealing". If not, it's obviously a big limitation for programmers. Theoretically, the seal is the most perfect, but in application, it can improve the flexibility and efficiency of the program if some preferential treatment is given to the subcategories. ,VB offers three options for solving the dilemma of the Fish and paws:

(1) public── specify certain data as public, any procedure can be directly used; At this time, there is no sealing effect.
(2) protected── designate certain information to be family-owned, that is, only procedures within the category of descendants are desirable, and procedures for non-descendant categories must be called into the family's procedures for access.
(3) It is advisable for the private── to designate a particular information to be classified as private and to be limited to the class. Subclasses of the program must also call the parent class to access the program, at this time with a fully sealed.

When you introduced the basic concept of "encapsulation" earlier, you already know the intent of public and private, and protected is used in conjunction with inheritance, so it is particularly emphasized here.
VB to the reality of compromise, opened the door, not completely sealed; so some people think VB is not the perfect OOP language. What do you think? Please look at a program:

' Ex01.bas
Imports System.ComponentModel
Imports System.Drawing
Imports system.winforms
'---------------------------------------------------------
Class person
Private name as String
Public Age as Integer
Public Sub New (ByVal na as String, ByVal A as Integer)
name = NA
Age = A
End Sub
End Class

Class Teacher
Inherits person

Public salary as single
Public Sub New (ByVal na as String, ByVal an as Integer, ByVal sa as single)
MyBase.New (Na, a)
Salary = sa
End Sub
Public Sub Modifyage (ByVal A as Integer)
Age = A
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 Crag as New Teacher ("Crag", 38, 45000)
Crag.modifyage (42)
MessageBox.Show ("Age:" + str (crag.age) + ", SALARY:"
+ str (crag.salary))
End Sub
End Class

The output of this program is as follows:
Age:42 salary:45000

The age data member of person is defined as public, to indicate that age is public data, and any program is accessible. Thus the name of age is used by the,teacher modifyage (). In contrast, in the teacher category, the name is not allowed, because name is defined as the private data for privatec, representing the class of person. Then look at the teacher category of salary data, which is defined as public, means common meaning. So Form1_Click () can obtain the salary value directly using the Crag.salary format. However, it cannot be written as:crag.name, because name is private data. For example, the following procedure is not correct:

' Ex02.bas
' Some Error here!
Imports System.ComponentModel
Imports System.Drawing
Imports system.winforms
'---------------------------------------------------------
Class person
Private name as String
Public Age as Integer
Public Sub New (ByVal na as String, ByVal A as Integer)
name = NA
Age = A
End Sub
End Class

Class Teacher
Inherits person

Public salary as single
Public Sub New (ByVal na as String, ByVal an as Integer, ByVal sa as single)
MyBase.New (Na, a)
Salary = sa
End Sub
Public Sub modifyname (ByVal na as String)
name = Na ' Error here!
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 Crag as New Teacher ("Crag", 38, 45000)
Crag.modifyname ("Crag Clinton")
MessageBox.Show ("Age:" + str (crag.age) + ", SALARY:"
+ str (crag.salary))
End Sub
End Class

Because name is the private data for the person class, this data name cannot be used in the Modifyname () of the subcategory teacher, so it is wrong. If the person category is defined as:

' Ex03.bas
Imports System.ComponentModel
Imports System.Drawing
Imports system.winforms
'---------------------------------------------------------
Class person
Protected name as String
Public Age as Integer

Public Sub New (ByVal na as String, ByVal A as Integer)
name = NA
Age = A
End Sub
Public Function GetName () as String
GetName = Name
End Function
End Class

Class Teacher
Inherits person

Public salary as single
Public Sub New (ByVal na as String, ByVal an as Integer, ByVal sa as single)
MyBase.New (Na, a)
Salary = sa
End Sub
Public Sub modifyname (ByVal na as String)
name = NA
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 Crag as New Teacher ("Crag", 38, 45000)
Crag.modifyname ("Crag Clinton")
MessageBox.Show ("Age:" + str (crag.age) + ", NAME:"
+ Crag. GetName ())
End Sub
End Class

This program outputs:
age:42, Name:crag Clinton

At this time,name for the family of common information, any person of the descendants of the category are desirable. However, the procedures outside the family, such as the Form1_Click () procedure, are still not used directly. If the above definition should read:

Class person
Protected name as String
Private Salary as Decimal
Public Age as Integer

Public Sub New (ByVal na as String, ByVal A as Integer)
name = NA
Age = A
End Sub
End Class

At this time,salary is a private category and no other category is allowed. Name is a private family and the category outside the family cannot be used. Age is public and any category is available.


1.2 Public and private programs
The previous section introduced: data members have Private, Protected and public points. Similarly, program members can be classified as Private, Protected and public. Although in application, program members are mostly defined as public, but when necessary, the process can also be defined as private or Protected. Private programs, like private data, are restricted to programs of that category to call. e.g.

' Ex04.bas
Imports System.ComponentModel
Imports System.Drawing
Imports system.winforms
'----------------------------------------------------------------------
Class person
Private name as String
Private Age as Integer
Private Sub Modifyage (ByVal A as Integer)
Age = A
End Sub
Public Sub New (ByVal na as String, ByVal A as Integer)
name = NA
Age = A
End Sub
Public Sub Modify (ByVal na as String, ByVal A as Integer)
name = NA
Modifyage (a)
End Sub
Public Sub Show ()
MessageBox.Show (' name: ' + name + ' Age: ' + str (age))
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 P1 as New person ("David", 25)
P1. Show ()
P1.modify ("David Smith", 28)
P1. Show ()
End Sub
End Class


This program outputs:
Name:david age:25
Name:david Smith age:45

Modifyage () is a public utility for private programs,new () and modify (). Modifyage () is a program member of the person class, so modify () can call Modifyage (). Functions outside of the person category cannot call Modifyage (). For example: in Form1_Click (), you can write P1.modify (), because modify () is a utility. But in Form1_Click (), you cannot write P1.modifyage (), because Modifyage () is a private function. If the restriction on modifyage () is relaxed so that a subcategory of person can call Modifyage (), it is necessary to define modifyage () as the protected function, as follows:

' Ex05.bas
Imports System.ComponentModel
Imports System.Drawing
Imports system.winforms
'----------------------------------------------------------------------
Class person
Protected name as String
Private Age as Integer
Protected Sub Modifyage (ByVal A as Integer)
Age = A
End Sub
Public Sub New (ByVal na as String, ByVal A as Integer)
name = NA
Age = A
End Sub
Public Sub Show ()
MessageBox.Show (' name: ' + name + ' Age: ' + str (age))
End Sub
End Class

Class Teacher
Inherits person

Public Sub New (ByVal na as String, ByVal A as Integer)
MyBase.New (Na, a)
End Sub
Public Sub Modify (ByVal na as String, ByVal A as Integer)
Mybase.name = NA
Mybase.modifyage (a)
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 P1 as New Teacher ("David", 25)
P1. Show ()
P1.modify ("Kent Smith", 45)
P1. Show ()
End Sub
End Class


This program outputs:
Name:david age:25
Name:kent Smith age:45

At this time, the function of the descendant category can call Modifyage (), but the category outside the family is still not able to call it. summed up as simple rules:
If any category is allowed to be used, it is declared public.
If a subcategory is allowed, it is declared as protected.
If this category is allowed to be used, it is declared private.
Take a look at an example:

' Ex06.bas
Imports System.ComponentModel
Imports System.Drawing
Imports system.winforms
'----------------------------------------------------------------------
Class person
Protected name as String
Private Age as Integer
Protected Sub Modifyage (ByVal A as Integer)
Age = A
End Sub
Public Sub New (ByVal na as String, ByVal A as Integer)
name = NA
Age = A
End Sub
Public Sub Show ()
MessageBox.Show (' name: ' + name + ' Age: ' + str (age))
End Sub
End Class

Class Teacher
Inherits person

Public Sub New (ByVal na as String, ByVal A as Integer)
MyBase.New (Na, a)
End Sub
Protected Sub Modify (ByVal na as String, ByVal A as Integer)
Mybase.name = NA
Mybase.modifyage (a)
End Sub
End Class

Class Fulltime_teacher
Inherits Teacher

Public Sub New (ByVal na as String, ByVal A as Integer)
MyBase.New (Na, a)
End Sub
Public Sub modifyvalue (ByVal na as String, ByVal A as Integer)
Mybase.modify (Na, a)
End Sub
Public Sub modifydata (ByVal na as String, ByVal A as Integer)
Mybase.name = NA
Mybase.modifyage (a)
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 P1 as New fulltime_teacher ("David", 25)
P1. Show ()
P1.modifyvalue ("Kent Smith", 45)
P1. Show ()
End Sub
End Class

This program outputs:
Name:david age:25
Name:kent Smith age:45


Show () is the public program of the person category, the grandson category Fulltime_teacher inherited, become the Fulltime_teacher category of public program, so Form1_Click () program can write instructions: P1. Show (). Name and Modifyage () are protected members of person who can use them directly in teacher Modify () programs. and Fulltime_teacher can use them directly in the Modifydata () program. But Form1_Click () will not be able to use them. Modify () is a protected member of the teacher, who can use them directly in the Modifyvalue () program of the Fulltime_teacher, but Form1_Click () is not available. Therefore, if the above Form1_Click () Directive is changed to read as follows, it is not correct:

Protected Sub Form1_Click (...)
Dim P1 as New fulltime_teacher ("David", 25)
P1. Show ()
P1.modify ("Kent Smith", "error!")
P1.modifyage ' error!
P1. Show ()
End SubEnd Class
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.