Summary: VB is the language of the door-oriented object design, which has four main features: abstraction,
Package, inherit, polymorphism. This article is mainly explained in an example in encapsulation.
Operating system: Windows 2000 Advanced Server
Programming Tools: Visual Basic 6.0
Here we will understand its encapsulation through a specific example!
In the object, the object can be seen as a package of a dedicated operation on these attributes.
The package is an information shielding technique, the purpose of the package is to separate the definition of the object and the implementation of the implementation.
Step1:
1.1 New project is named VBOOP;
1.2 Click the Project Menu, select the Add Class Module and click the OK button;
1.3 Change the name of the class to TSCORE in its properties window.
Step2: Edit TSCORE Class Module Code
2.1. Here is the TSCORE class define four private (private) variables, they can only be in this module
It is visible, that is, some members of the class are hidden, and the user can only pass the attribute process.
Or function and other methods to access objects.
Basic syntax defining variables:
PRIVATE / PUBLC
Code section:
Private fname as string 'student's name
Private fmath as salesle 'mathematics score
Private fenglish as single 'English grades
2.2. Define six common (Property) processes for TSCORE classes and one
Method function calculating the total score.
Basic syntax of the definition method:
Private / Publc Property Get
Private / Public Property Let
PRIVATE / PUBLC Function
GET: Assign the value of the private variable in the module to the attribute process, usually referred to as reading;
Let: The private variable value in the module is defined through the property process, which is often referred to as writing.
Code section:
Public property Get GetName () AS STRING
GetName = FNAME
End Property
Public property let setName (Byval Name as string)
FNAME = Name
End Property
Public property Get getMath () AS Single
Getmath = fmath
End Property
Public property let setmath (Byval Math As Single)
Fmath = Math
End Property
Public property Get GeTENGLISH () AS Single
GetENGLISH = fenglish
End Property
Public property let setenglish (byval english as single) (BYVAL ENGLISH AS SINGLE)
Fenglish = English
End Property
Public Function Total () AS Single 'Computing Total Compraity Function
Total = getMath getENGLISH
END FUNCTION
Step3: Back to the Form1 window, add 12 controls on the window:
3.1 Add 5 text boxes TXTNAME, TXTMATH, TXTEN, TXTTOAL
3.2 Add 5 labels Labname, Labmath, Laben, Labtotal
Its CAPTION attribute is name, mathematics, English, and total score;
3.3 Add 2 command buttons ComsetValues, COMSEARCH its CAPTION attribute is assigned, query, respectively.
Step4: Edit window event
4.1. Construct the score object and query keyword searchkey. Object-oriented method
In this way, we can say that the definition is to define the data type, and the statement is
The like is a declared variable. That is, the object is actually a variable.
Dim score as new tscore
DIM Searchkey As String
4.2. Click event to four private variables in the module
Private sub comsetvalues_click ()
If Val (txtmath.text)> = 0 and val (txtmath.text) <= 100
And Val (txten.text)> = 0 and val (txten.text) <= 100
THEN
WITH SCORE
.Setname = txtname.text
.SETMATH = VAL (txtmath.text)
.Senglish = val (txten.text)
End with
TXTNAME.TEXT = "" "
TXTMATH.TEXT = "" "
TXten.Text = ""
Print name: "& score.getname &" Mathematics: "& score.getmath &" English: "& score.Getenglish
Else
MsgBox "value range: [0,100]", 64, "Tips"
END IF
End Sub
4.3. Query all kinds of results and total clicks
Private subsearch_click ()
SearchKey = INPUTBOX ("Please enter the name keyword you want to query:", "Query")
If Word = score.getname kil
TXttotal.Text = Score.Total
TXTNAME.TEXT = Score.GetName
TXTMATH.TEXT = Score.GetMath
Txten.text = score.Getenglish
TXttotal.visible = true
Labtotal.visible = true
Else
"Sorry! No this record!", 64, "Tips"
END IF
End Sub
4.4. Window load event
Private sub flow_load ()
TXTTOTAL.VISIBLE = FALSE
Labtotal.visible = false
End Sub
4.5 Window Close the event, here we will release the object variable, otherwise the memory disclosure will occur
Private Sub Form_Unload (Cancel AS Integer)
Set score = Nothing
End Sub
Step5: When you are here, the program is all written, you can run it as needed to press F5.