Vb. NET Chinese Tutorials (1) Categories and encapsulation

Source: Internet
Author: User
Tags definition class definition explode integer
Package | tutorial | Chinese 1. Program members for categories (Procedure member)

The task of Class (class) is to organize and encapsulate information (data) and programs (Procedure). The category tells the computer:"that the object should contain those data, and that it should contain those programs to deal with the outside message. The class shall specify its information and procedures, which we refer to as the "data member" of the category, which is referred to as the "program member" of the Class (Procedure). The description of the category content is the so-called category definition (class definition). The format of the category definition is--

The purpose of the class is to declare the object. e.g.

' Ex01.bas
Imports System.ComponentModel
Imports System.Drawing
Imports system.winforms
'----------------------------------------------------
Class Tree
Public Varity as String
Public Age as Integer
Public height as single
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 A as New tree ()
MsgBox ("Object A is Created.")
End Sub
End Class

This program defines the category tree, it contains only information but no program, for a "Yangchun type" category. When the computer executes the declaration instruction in the Form1_Click () program--
Dim A as New tree ()

Give object a the memory space allocated enough to hold the 3 data. However, this tree category has only information and no program. Therefore, object A cannot accept the foreign message. At this point, you can add program members so that the tree category contains programs, power, and objects have the ability to process messages. e.g.

' Ex02.bas
Imports System.ComponentModel
Imports System.Drawing
Imports system.winforms
'------------------------------------------------------------
Class Tree
Public Varity as String
Public Age as Integer
Public height as single
Public Sub-Input (ByVal hei as single)
Height = Hei
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 A as New tree ()
A.input (2.1)
MessageBox.Show ("Set a.height to 2.1", "hello!")
End Sub
End Class

This program output: Set a.height to 2.1
Now the,tree category already has program member input (). The program member's writing is identical with the general VB procedure, only it should declare in the category, becomes the category the exclusive procedure. At the moment, object A contains 3 items and one procedure:

The computer executes to the command--
A.input (2.1)

The message ──input (2.1) is passed to object A. At this point, the computer calls and executes the input () program in Object A. The input () in object A is defined as the input () in the tree category, so Form1_Click () passes the argument ──2.1 to the HEI variable in input ().



Next, the narrative--
Height = Hei

The HEI variable value is deposited in the data member ──height of object A.



At the moment, object A's processing of the message has been completed, and its internal data has changed, i.e. the internal state of object A (Internal) has changed; This is one of the object's behaviors. You are already adding a program that, in the same way, continues to add other programs to make the object more colourful. e.g.

' Ex03.bas
Imports System.ComponentModel
Imports System.Drawing
Imports system.winforms
'-----------------------------------------------------------------------
Class Tree
Public Varity as String
Public Age as Integer
Public height as single
Public Sub-Input (ByVal hei as single)
Height = Hei
End Sub
Public Function inquireheight () as single
Inquireheight = height
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 A as Tree = New tree ()
Dim h as single
A.input (2.1)
h = a.inquireheight ()
MessageBox.Show ("height =" + str (h) + "metric", "hi!")
End Sub
End Class

This program outputs the following:height = 2.1 m
The tree category has 2 program Members ──input () and Inquireheight (). A program member of a class must be used in conjunction with its object. The format is:

That is, it must be in the form of a message. e.g.

If a program member does not match the object, what does the computer do? for example:

' Ex04.bas
' Some Error here!
Imports System.ComponentModel
Imports System.Drawing
Imports system.winforms
'--------------------------------------------------------------
Class Tree
Public Varity as String
Public Age as Integer
Public height as single
Public Sub-Input (ByVal hei as single)
Height = Hei
End Sub
Public Function inquireheight () as single
Inquireheight = height
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 A as Tree = New tree ()
Dim h as single
A.input (2.1)
h = inquireheight ()
MessageBox.Show ("height =" + str (h) + "metric", "hi!")
End Sub
End Class

When the computer sees the instructions in the Form1_Click ()
h = inquireheight ()

It regards inquireheight () as a separate procedure, independent of the Inquireheight () in the tree category, so the computer is looking for the definition of this inquireheight (), but it is not, so the program is wrong. Therefore, you have to master the principle that the program member's unique task is to support the behavior of the object, must be used in conjunction with the object.
The concept of 2."encapsulation"

objects are organized and "encapsulated" (encapsulate) of data and programs, and can be used only in specific ways to use categories of data members and program members. Objects are like handbags, which can only be accessed from a fixed opening, or you will not be able to put your money in your handbag. The object is like a "firewall" to protect the data in the category so that it is unaffected by the outside world. Think of the Great Wall of our country can protect the people in the Hunni, to avoid infringement by the invasion, but the wall is not completely closed, and there are Shanhaiguan, the entrance of the Yumen customs. Objects and the Great Wall function is consistent, it protects its data members, but also has a normal data access pipeline: To program members to access data members. Please look at a program:

' Ex05.bas
Imports System.ComponentModel
Imports System.Drawing
Imports system.winforms
'-------------------------------------------------------
Class Tree
Public Varity as String
Public Age as Integer
Public height as single
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 A as New tree ()
A.height = 2.1
MessageBox.Show ("height =" + str (a.height) + "metric")
End Sub
End Class

This program outputs the following:height = 2.1 m
The,tree class in this program contains 3 data members, that is, the object contains 3 data values, which can be accessed directly by the program members of this class. At the same time, other programs are allowed to access the value of the data member, whose access format is:

e.g.
A.height = 2.1

This instruction deposits 2.1 in the height variable of object A. So the content of object A is:



See a common error as follows:

' Ex06.bas
' Some Error here!
Imports System.ComponentModel
Imports System.Drawing
Imports system.winforms
'-------------------- ------------------------------------
Class Tree
Public Varity as String
Public Age as Integer
Public height as single
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 A as New tree ()
Height = 2.1
MessageBox.Show ("height =" + str (a.height) + "metric")
End Sub
End Class

Form1_Click () The instruction in the program ──height = 2.1, this height variable is not used in conjunction with the object, the computer does not think it is the tree category of the height variable. The computer regards it as the Form1_Click () automatic variable (Automatic Variable), but has not seen its declaration, so the procedure is wrong! This is the case where objects are most loosely protected from their data members because programs other than the object's category (that is, tree), such as the form1_ The Click () program) can still access the contents of the data member. Like a bomb, in addition to the fuse tube, there are many pipelines to explode the chemical in the bomb; you will not be able to put the bomb on the plane, because when it will explode will not be controlled. Similarly,tree category of data ──height variable, even the external form1_click () can change it at will; then one day the contents of the height of the problem, it would be difficult to trace the cause of the error, this procedure will make you a big headache, because you are no longer in control of the situation.
Now in the VB program, can take more stringent protection measures, so that you can control the information in the category of changes in the situation. e.g.

' Ex07.bas
' Some Error here!
Imports System.ComponentModel
Imports System.Drawing
Imports system.winforms
'-----------------------------------------
Class Tree
Private Varity as String
Private Age as Integer
Private Height as single
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
Public Sub Form1_Click (ByVal sender as Object, ByVal
e as System.EventArgs)
Dim A as New tree ()
A.height = 2.1
MessageBox.Show ("height =" + str (a.height))
End Sub
End Class

This procedure changes the original public special word to private, to take strict protective measures against the data members of the tree category. The difference between public and private is--

Public indicates that programs outside this category can access data members.
Private means that programs outside this category must not directly access data members, only program members can access data members.

So, the computer sees the instruction ──a.height = 2.1, because the tree category takes strict protection (private), the Form1_Click () program cannot use the height variable name. So the instruction ──a.height = 2.1 wrong. Perhaps you ask: Is this not access to the data members in the category? The answer is that the member Function of a program in the:"category can access the data member, and the program outside the class can access the data member on behalf of the program member. 」

Figure 1, type of communication pipeline--Program members

This is like, the fuse tube can cause the bomb within the chemical explosion, people can only be detonated through the fuse, so that people feel the use of bombs is safe and simple. Similarly, the object is communicated through the program members and the outside world, which can reduce the accidental destruction of the information within the object (accidentally detonated the bomb). e.g.

' Ex08.bas
Imports System.ComponentModel
Imports System.Drawing
Imports system.winforms
'--------------------------------------------------
Class Tree
Private Varity as String
Private Age as Integer
Private Height as single
Public Sub-Input (ByVal hei as single)
Height = Hei
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 A as New tree ()
A.input (2.1)
MessageBox.Show ("OK")
End Sub
End Class

The input () is placed in the tree category and is the program member of the tree, which can access the value of the data member height. The program declaring the input () program as public means to call it, and its calling format is--

The simple rule is:
Public indicates that a program authorized to the outside world calls program members by this format.

If this program is rewritten as:



' Ex09.bas
' Some Error here!
Imports System.ComponentModel
Imports System.Drawing
Imports system.winforms
'----------------------------------------------
Class Tree
Private Varity as String
Private Age as Integer
Private Height as single
Private Sub input (ByVal hei as single)
Height = Hei
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 A as New tree ()
A.input (2.1)
MessageBox ("OK")
End Sub
End Class

This procedure is problematic because input () is a private program member of the tree category rather than a public program member, so you cannot use the following format-



So this program is wrong. Please look at another example!

' Ex10.bas
Imports System.ComponentModel
Imports System.Drawing
Imports system.winforms
'-------------------------------------------
Class Tree
Private Varity as String
Private Height as single
Public Age as Integer
Public Sub Showage ()
MessageBox.Show (' 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 A as New tree ()
A.age = 8
A.age = A.age + 2
A.showage ()
End Sub
End Class

The tree category contains 2 private members ──variety and height, and has 2 public members ──age and Showage (). Because age is a public data member, Fom1_click () can use the format-

To access the age variable in the tree. The instruction ──a.age = 8 puts 8 into the age variable in object A. Instruction ──a.age = a.age + 2 makes object A's age variable value plus 2, become 10. Because the Showage () program is a public program member, you can also use the format-

To call the Showage () program.
Because the purpose of a category (i.e. an object) is to protect the data, and to provide program members to communicate with the outside world (accept, process, and respond to messages). Typically, data members are declared private, and program members are declared public. That is, try to use less format--

and try to use a lot of formatting--

e.g.

' Ex11.bas
Imports System.ComponentModel
Imports System.Drawing
Imports system.winforms
'--------------------------------------------------------------------
Class Tree
Private Varity as String
Private Age as Integer
Private Height as single
Public Sub input (ByVal v as String, ByVal an as Integer, ByVal Hei as single)
Varity = V
Age = A
Height = Hei
End Sub
Public Sub Show ()
MessageBox.Show (Varity + "," + str (age) + "," + str (height))
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 A, B as New tree ()
A.input ("Peach", 8, 2.1)
B.input ("Pinapple", 2, 0.5)
A.show ()
B.show ()
End Sub
End Class

This VB program,tree the data members ──variety, age and height are private members, and the input () and show () program is public members. Form1_Click () program, the first birth of the object ──a and B. Next, the directive--



The contents of the 3 items are deposited in the information member of object a,a:



In the same way, directives--
B.input ("Pineapple", 2, 0.5)

The 3 items are also deposited in object B, and B is the following:



Finally, call the show () program to display the contents of object A and object B:

Peach, 8, 2.1
Pineapple, 2,. 5
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.