Specifically, VB6 cannot implement class inheritance in the true sense (for example, inheritance in C ++), but implements also provides similar functions.
We first create a class module cbase. CLs
The Code is as follows:
Private mvarbaseproperty as string
Public sub basefunction ()
Msgbox "Hello world !"
End sub
Public property let baseproperty (byval vdata as string)
Mvarbaseproperty = vdata
End Property
Public property get baseproperty () as string
Baseproperty = mvarbaseproperty
End Property
Next we will create a new type of module (cinherit. CLs). The Code is as follows, with key comments.
Implements cbase ''' note this keyword
Dim m_baseproperty as string
''''---------------------------------------------------------------------
''' The code between the dotted lines is inherited from the cbase class.
''' Note that the format is as follows: base class _ attribute name (or method name)
'''' The Declaration keyword of its method is also changed to private
Private property get cbase_baseproperty () as string
Baseproperty = m_baseproperty
End Property
Private property let cbase_baseproperty (byval vdata as string)
M_baseproperty = vdata
End Property
Private sub cbase_basefunction ()
Msgbox "inherit"
End sub
''''---------------------------------------------------------------------
''' This method inherits the class's own method.
Public sub inheritmsg ()
Msgbox "My owner MSG"
End sub
Now we create a new form for testing. We place the test code in the form_load event.
Test 1:
Dim objtest as cbase
Set objtest = new cbase
With objtest
. Basefunction
End
Set objtest = nothing
Run the program. The base is displayed, indicating that the basefunction function in CBSE is called.
Test 2:
Dim objtest as cbase
Set objtest = new cinherit
With objtest
. Basefunction
End
Set objtest = nothing
Run the program and pop up inherit, which indicates that the base function in cinherit is called.
Test 3:
Dim objtest as cinherit
Set objtest = new cinherit
With objtest
. Inheritmsg
End
Set objtest = nothing
Run the program. The my owner function is displayed, indicating that the inherited class can use its own functions or attributes.